Networking :: How to Detect Lost Network Connection



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...

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.

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"

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?

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.
Next Page...
original here.