Script to connect to internet at bootup

Olivier Cailloux olivier.cailloux at gmail.com
Tue Mar 8 01:58:03 UTC 2011


Le 05/03/2011 18:45, Fajar Priyanto a écrit :
> On Sat, Mar 5, 2011 at 11:53 PM, Olivier Cailloux
> <olivier.cailloux at gmail.com>  wrote:
>
>> So one way to go would be to further explore that suggestion by testing in
>> some loop, or with some other tools, whether the DNS resolves (some hints
>> are given on the page you suggested). Or simply run my curl command
>> repeatedly in a loop until it works (with a limit on the number of tries).
>> I'll do this if I don't get any better suggestion on this list.
>>
>> However my one-line script is becoming more and more complex, it feels
>> strange to me to have to manually implement a check in a loop just to
>> specify a dependency on the DNS service. I still wonder if I could use
>> something like the dependency mechanism init.d uses (AFAIU), or anything of
>> the same kind.
> Well, IMHO, in this case there are only two choices:
> 1. Since you know roughly how long it takes for the internet
> connection to be established, say.. 20 seconds from booting, then the
> delay method is the most efficient way to accomplish what you want.
> 2. But if you don't want the first method, the only way is to tell the
> OS to check the internet connection in a loop then authenticate, or do
> the authentication in a loop. You can incorporate this with beep sound
> if you want. Just to inform you nicely when the authentication is
> successful.
> https://launchpad.net/ubuntu/+source/beep
>
Thanks again to all those who responded.

For those who might be interested: I opted for the authentication in a 
loop, and rewrote the script in python. It is launched by cron: 
"@reboot   olivier    
/home/olivier/Logiciels/FBL-auth/FBL-curl-auth.py", with the script being:
--
#!/usr/bin/env python

import time
import subprocess
import os
import sys

LOG_FILE_STR = "/home/olivier/Logiciels/FBL-auth/FBL-curl-auth"
CURL_EXEC = "/usr/bin/curl"
MAX_TRIALS = 2
WAIT_SECONDS = 10

logOut = open(LOG_FILE_STR + "-out.log", "w")
logResName = LOG_FILE_STR + "-res.log"
logErr = open(LOG_FILE_STR + "-err.log", "a")

print >> logOut, os.path.abspath(sys.argv[0]), "started."

curlCommand = []
curlCommand.append(CURL_EXEC)
curlCommand.append("--silent")
curlCommand.append("--show-error")
curlCommand.append("-d")
curlCommand.append("admin_id=...")
curlCommand.append("-d")
curlCommand.append("admin_pw=...")
curlCommand.append("http://internet.ciup.fr/web_auth/index.html")
curlCommand.append("-o")
curlCommand.append(logResName)

nbTrials = 1

while(True):
     curlReturn = subprocess.call(curlCommand, executable = CURL_EXEC, 
stdout = logOut, stderr = logErr)
     if (curlReturn != 0):
         if (nbTrials < MAX_TRIALS):
             print >> logOut, "curl trial number", nbTrials, "failed, 
looping."
             nbTrials += 1
             time.sleep(WAIT_SECONDS)
         else:
             print >> logErr, "curl trial number", nbTrials, "failed, 
ending."
             break
     else:
         print >> logOut, "curl succeeded, ending."
         break

print >> logOut, "Ended."

logOut.close()
logErr.close()
--

A few tests seem to show that it works. Oddly enough, it seems like now 
I don't get any failure any more (curl works first time)!
Olivier





More information about the ubuntu-users mailing list