Announcement

Collapse
No announcement yet.

Force Ethernet Reconnect on Raspberry Pi after Router WAN connection reset

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Force Ethernet Reconnect on Raspberry Pi after Router WAN connection reset

    Today a guy from BT was fiddling with the junction box outside, and my modem dropped connection to my router.

    At the time, the router did not automatically force reconnect (my fault, hadn't configured it to do so). When I noticed what had happened, I reconnected to the modem. So far so good.

    A couple of hours later, I noticed that two of my three Pi (all of which are connected with ethernet cables) had not reconnected to the router. The one that did reconnect is running Raspbmc (XBMC port to Raspberry Pi); the two that did not are running Apache with some bits on top.

    This is a pain because not only did it take the services offline, but I was unable to ssh to the Pi to correct the problem. I'm not sure if removing and reconnecting the ethernet cable would have worked, in the end I just pulled the power and rebooted.

    I found a thread on the RasPi forums about reconnecting WiFi with a BASH script after a drop. My guess is that Raspbmc includes something similar:

    Code:
    #!/bin/bash
    
    while true ; do
       if ifconfig wlan0 | grep -q "inet addr:" ; then
          sleep 60
       else
          echo "Network connection down! Attempting reconnection."
          ifup --force wlan0
          sleep 10
       fi
    done
    I'd have to modify this for ethernet instead of wlan, but I guess it would work.

    The thread mentions either putting this in /etc/rc.local, or using cron to execute something similar (presumably just the ifup bit, without the sleep parts) at a given interval.

    I was wondering which is more efficient, the cron way or running a script in the background?

    Feathers
    samhobbs.co.uk

    #2
    Have written myself a script that works for resetting a deliberately dropped connection. I chose to run it with cron every 5 minutes in the end. May decrease the frequency later but I'm interested to see if the problem reoccurs, and if it does whether or not the script solves the problem (the logfile should help with this, the more data the better first time around).

    Code:
     #!/bin/bash
     
    LOGFILE=/home/admin/network-monitor.log
     
    if ifconfig eth0 | grep -q "inet addr:" ;
    then
            echo "$(date "+%m %d %Y %T") : Ethernet OK" >> $LOGFILE
    else
            echo "$(date "+%m %d %Y %T") : Ethernet connection down! Attempting reconnection." >> $LOGFILE
            ifup --force eth0
            OUT=$? #save exit status of last command to decide what to do next
            if [ $OUT -eq 0 ] ; then
                    STATE=$(ifconfig eth0 | grep "inet addr:")
                    echo "$(date "+%m %d %Y %T") : Network connection reset. Current state is" $STATE >> $LOGFILE
            else
                    echo "$(date "+%m %d %Y %T") : Failed to reset ethernet connection" >> $LOGFILE
            fi
    fi
    More here:
    http://www.samhobbs.co.uk/2013/11/fi...-raspberry-pi/

    Feathers
    samhobbs.co.uk

    Comment

    Working...
    X