How to Detect Lost Network Connection


Forum: Networking
Topic: How to Detect Lost Network Connection
started by: Juanito

Posted by Juanito on Feb. 17 2008,04:28
I'm running dsl-3.4.11 on a desktop that acts as a printer/scanner/music server for the family. For some reason, the desktop loses its network connection to the router a couple of times a week (I originally thought this was due to WDef's citytime, but no ). If I'm around this is not a big deal, but if the desktop loses the connection to the network whilst I'm away, then the family can't print until I get back.

I'd like to write a script that checks for the network connection periodically and invokes the dsl panel netcardconf script to reconnect the network if it finds it down (and restart cupsd, smbd, nmbd, saned, firefly...)

The problem is that I cannot figure out what to check (ping the router?) or how to do it in scripting terms. I'd be grateful for any ideas...

Posted by curaga on Feb. 17 2008,09:18
I've done a similar thing. Pinging the router is a good idea, as it's close.
Just create an infinite loop and run it in the background, something like
Quote
while [ 1 ]; do
sleep 60
if ! ping -c2 router 2>&1 | grep ttl; then

reconnect here

fi
done

The ping looks weird b/c ping doesn't always return 0 on success.

Posted by Jason W on Feb. 17 2008,19:20
Curaga has the right idea, and here is how I would do it with my 192.168.0.1 router:


Code Sample

#!/bin/bash

while [ 1 ]; do
sleep 60
if ! nslookup 192.168.0.1 | grep 192.168.0.1 > /dev/null 2>&1; then

  if ! ifconfig | grep eth0; then
    /sbin/ifconfig eth0 up > /dev/null 2>&1
  fi

     route add default gw 192.168.0.1
     /etc/init.d/networking restart > /dev/null 2>&1 &&

    if ps | grep cupsd > /dev/null 2>&1; then
      /etc/init.d/cupsys restart > /dev/null 2>&1
    fi

    if ps | grep smbd > /dev/null 2>&1; then
     killall /usr/sbin/smbd
     /usr/sbin/smbd
    fi

    if ps | grep mt-daapd > /dev/null 2>&1; then
     killall mt-daapd
     mt-daapd -y
    fi

    #--etc,etc,etc--

   else
   sleep 5

fi
done



"/etc/init.d/networking restart" should restart the connection without the need to configure the connection again.

If gnu-utils are loaded, then the ps command should be "ps -A"  instead of just "ps"

Posted by Juanito on Feb. 22 2008,10:52
Thanks for the suggestions, but:
Code Sample
$ sudo /sbin/ifconfig eth0 up
$ sudo /etc/init.d/networking restart

doesn't do the trick.

I tried looking through netcardconf.lua but could not decode what selecting "use dhcp broadcast?" does in terms of commands (since it appears I need to select this in order to get the network reconnected).

I really don't know much about networking, should dhcp broadcast be required to reconnect to a router that is passing a fixed ip address?

Posted by curaga on Feb. 22 2008,11:19
Well, a fixed dhcp is still dhcp, so it needs to be on. The comp can't know wether it will get a fixed or a random address.
Posted by Juanito on Feb. 22 2008,11:58
Thanks - so in terms of a command in a script how do I turn dhcp on?
Posted by Jason W on Feb. 22 2008,12:48
Juanito,
 You're right, looking closer at it /etc/init.d/networking will not usually help.  I saw an old forum post and pump seems to be the dhcp client that requests an address -- sudo pump -i eth0 -- .

HTH

Posted by Juanito on Feb. 23 2008,09:49
Thanks - "sudo pump -i eth0" did the trick from the command line.

I've started a script running - let's see if the desktop can stay connected for a few days now...

Posted by Juanito on Mar. 01 2008,10:12
After a couple of weeks testing, this got the job done:
Code Sample
#!/bin/bash
#
while [ 1 ]; do
sleep 60
if ! ping -c2 192.168.1.1 2>&1 | grep ttl; then
pump -i eth0
fi
done
#
#EOF

It seems that keeping the network connection is enough to keep cupsd, smbd, nmbd, etc, etc running.

Thanks for the help

Posted by Juanito on Mar. 26 2008,07:37
OK, so running the above script from a terminal window once dsl has booted works fine.

However...running it from bootlocal.sh does not work fine as command is not passed back to bootlocal.sh so that the boot process can finish.

How do I pass the above script to run in the background from bootlocal.sh?

Posted by ^thehatsrule^ on Mar. 26 2008,14:43
Appending the command with & in the shell means to background it.  For example,

myprog &

See .xinitrc for more examples.

Posted by Juanito on Mar. 28 2008,10:26
Thanks, modifying the line in bootlocal.sh to read:
Code Sample
/opt/mynet_connect.sh &

...solved the problem.

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.