Re: How to detect network offline and execute script
High,
On Tue, 7 Aug 2001, David Morris wrote:
> I know this is something I have seen somewhere (and it is probably something
> right in front of my eyes). But I have just setup a cable modem that uses
> dhclient to grab it's IP. Every once in a while the cable modem will flake out
> and then it is unusable - until I sit down and either physically reboot it, or
> (I've figured this out) release the IP and drop the NIC then reconnect the
> card and the dhclient.
>
> What I want is a script I can run as a cron job (every 15 minutes or so) that
> will check to see if the network is usable or not. If the modem has flaked out
> on me, it will bring it down and then keep trying to bring it up until it
> succeeds. (Oh and it would be nice to have it send me a little email telling
> me what it did - that's not asking too much is it? :).
>
> Like I said, I know this has got to be somewhere, but I can't find it. Can
> someone point to where it is laid out for me or maybe put together a little
> script fragment that will do the checking for me?
>
It is really very simple. Add these lines to /etc/crontab:
0 * * * * root /bin/ping -c 2 -i 5 213.10.33.89 > /dev/null || /etc/init.d/adsl restart
15 * * * * root /bin/ping -c 2 -i 5 213.10.33.89 > /dev/null || /etc/init.d/adsl restart
30 * * * * root /bin/ping -c 2 -i 5 213.10.33.89 > /dev/null || /etc/init.d/adsl restart
45 * * * * root /bin/ping -c 2 -i 5 213.10.33.89 > /dev/null || /etc/init.d/adsl restart
where 213.10.33.89 is the closest ip address and /etc/init.d/adsl is your
connection script.
The big prob is to find the proper IP address. It is not ok if you use the
IP of your modem, because Linux uses a shortcut then. I think it is quite
safe to take the first ip after your network, i.e. do a traceroute to
somewhere and write down the first ip address after your network (this is
probably your isp's). Do this with some sessions in a couple of days. If
you are sure enough that the address does not change, use that.
Also note that if you have a slow connection you have to play with the
ping respond times. Perhaps you could even setup a traceroute kind of
script in stead of ping.
Greetz,
Sebastiaan
Reply to: