Script to connect to internet at bootup

Olivier Cailloux olivier.cailloux at gmail.com
Sat Mar 5 15:53:42 UTC 2011


Le 05/03/2011 02:26, Fajar Priyanto a écrit :
> On Sat, Mar 5, 2011 at 3:48 AM, Olivier Cailloux
> <olivier.cailloux at gmail.com>  wrote:
>> Also the script works if I set a delay of 15 seconds before running it. But
>> I'd like to find some clean way of configuring this, that is, I'd like to be
>> able to specify to run the script only when "everything needed" (meaning, I
>> guess: the DNS system) is ready. That's why I thought I'd use the dependency
>> mechanism of init.d (see above the original post), but there may exist
>> better approaches.
>> Olivier
> So, you are correct. It's timing issue. Delaying the script makes it work.
> Well, if you don't want to use the delay method, we can use command to
> resolve something, and if it doesn't succeed, keep trying until
> successful, then run the authenticate script.
>
> Some example I found in the net:
> WGET="/usr/bin/wget" $WGET -q --tries=10 --timeout=5
> http://www.google.com -O /tmp/index.google&>  /dev/null if [ ! -s
> /tmp/index.google ];then echo "no" else echo "yes" fi
>
> Read more: http://www.linuxscrew.com/2009/04/02/tiny-bash-scripts-check-internet-connection-availability/#ixzz1FgTn9iYo
>
> Also, I think better write all those thing in a separate script and
> call it through rc.local instead for tidiness.
>
I tried the following script, which does not work.
--
echo "${0}" start > /home/olivier/Logiciels/FBL-wg-auth-out.log
wget --tries=10 --retry-connrefused --timeout=5 
http://internet.ciup.fr/  1>> 
/home/olivier/Logiciels/FBL-wg-auth-out.log 2>> 
/home/olivier/Logiciels/FBL-wg-auth-err.log
if [ "$?" -ne "0" ]; then
     echo "Wget failed, ending." >> 
/home/olivier/Logiciels/FBL-wg-auth-err.log
else
     echo "Wget succeeded, continuing." >> 
/home/olivier/Logiciels/FBL-wg-auth-out.log
     curl (...)
fi
echo "${0}" end >> /home/olivier/Logiciels/FBL-wg-auth-out.log
--
Does not work because wget considers a DNS error as a permanent error 
and does not retry after the first failure.

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.

(Note that my question is not only about solving my specific current 
problem but also about learning how to effectively script things on a 
linux system.)
Olivier





More information about the ubuntu-users mailing list