reconnect.sh
Here is a Linux shell script to automatically reconnect a high speed Internet connection if you have been disconnected. It was written for OpenSuSE 10.2 with DSL connections and I haven't tested it anywhere else. However, it should be a good starting point for other systems.
It is meant to be run at startup in rc.local.
nohup nice sh /root/reconnect.sh &
On SuSE, boot.local cannot be used. Use a script like the following:
#!/bin/bash # restart_reconnect.sh TMP=`ps -ef` # don't include this script in the grep TMP=`echo "$TMP" | grep reconnect.sh | grep -v restart` echo "$TMP" if [ -z "$TMP" ] ; then nohup nice /bin/bash /root/reconnect.sh & fiand run it with the crontab command:
00,10,20,30,40,50 * * * * /bin/bash -c /root/restart_reconnect.sh
You many need to modify some of the variables at the start of the script to meet the requirements of your version of Linux.
#!/bin/bash
#
# reconnect.sh - check and reconnect a home high-speed internet connection
#
# January 19, 2007
# by Ken O. Burtch
#
# This was built for OpenSuSE 10.2 and DSL but it should be adaptable to other
# systems.
# This assumes you've configured your networking to use ifdown/up to control
# the high-speed connection.
#
# This script should be run from boot.local/rc.local.
# ---------------------------------------------------------------------------
set -u
# Configuration Settings (change as required)
CONNECTION="dsl0" # Name of the high-speed ifconfig device
SERVER="www.google.ca" # Name of a server to test the connection
SERVER2="www.yahoo.ca" # Backup test server
TMP="/tmp/reconnect.$$" # Where to build log messages
LOG="/root/reconnect.log" # The log
BACKUP_LOG="/root/reconnect.old" # The last (old) log
LOCK="/tmp/reconnect_lock.$$" # The lock file
# Commands (Correct these for your locations)
IFCONFIG="/sbin/ifconfig" # View connections
IFUP="/sbin/ifup" # Start a connection
IFDOWN="/sbin/ifdown" # Stop a connection
IFSTATUS="/sbin/ifstatus" # Connection status
PING="/bin/ping" # Network Ping
TRACEROUTE="/usr/sbin/traceroute" # Network Trace
# Other Variables
DSL=
PING_RESULT=
PING_RESULT2=
TRACE=
TRACE_HOSTS=
CNT=
OK=
# Sanity Tests - check user, lock file and commands
if [ "$LOGNAME" != "root" ] ; then
echo "$0: Must be root to run"
exit 1
fi
if [ -f "$LOCK" ] ; then
echo "$0: lock file $LOCK exists"
exit 1
fi
if test ! -x "$IFCONFIG" ; then
echo "$0: $IFCONFIG cannot be run"
exit 1
fi
if test ! -x "$IFUP" ; then
echo "$0: $IFUP cannot be run"
exit 1
fi
if test ! -x "$IFDOWN" ; then
echo "$0: $IFDOWN cannot be run"
exit 1
fi
if test ! -x "$IFSTATUS" ; then
echo "$0: $IFSTATUS cannot be run"
exit 1
fi
if test ! -x "$PING" ; then
echo "$0: $PING cannot be run"
exit 1
fi
if test ! -x "$TRACEROUTE" ; then
echo "$0: $TRACEROUTE cannot be run"
exit 1
fi
# Startup
if [ -f "$LOG" ] ; then # an existing log?
mv "$LOG" "$BACKUP_LOG" # back it up
fi
echo `date`": $0: restarted using PID $$" > "$LOG" # start new log
let "CNT=4" # force quality test
OK= # not sure we're OK
# Infinte loop
while true ; do
touch "$LOCK" # one at a time
# DSL Reconnect Test (using ifup)
DSL=`$IFCONFIG | fgrep "$CONNECTION"` # connect up?
if [ -z "$DSL" ] ; then # if not
OK= # we're not OK
echo `date`": --------------------------------------------------" >> "$TMP"
$IFSTATUS "$CONNECTION" >> "$TMP" 2>&1 # show status
echo `date`": reconnect $CONNECTION..." >> "$TMP" # info message
$IFDOWN "$CONNECTION" >> "$TMP" 2>&1 # clean shutdown
$IFUP "$CONNECTION" >> "$TMP" 2>&1 # and startup
DSL=`$IFCONFIG | fgrep "$CONNECTION"` # connection up?
if [ -z "$DSL" ] ; then # if not
echo `date`": RECONNECT FAILED" >> "$TMP" # fail message
else # otherwise
echo `date`": OK" >> "$TMP" # we're back
fi
fi
# Connection Quality Test
# only do this every 5th loop (10 minutes)
let "CNT++" # inc counter
if [ $CNT -ge 5 ] ; then # 5th time?
let "CNT=0" # reset counter
DSL=`$IFCONFIG | fgrep "$CONNECTION"` # connection up?
if [ ! -z "$DSL" ] ; then # then proceed
PING_RESULT=`$PING -c 1 "$SERVER" 2>&1` # see test server?
if [ $? -ne 0 ] ; then # no
PING_RESULT2=`$PING -c 1 "$SERVER2" 2>&1` # try secondary
if [ $? -ne 0 ] ; then # still no?
OK= # not OK
echo `date`": --------------------------------------------------" >> "$TMP"
echo `date`": cannot reach $SERVER or $SERVER2 (via ping)..." >> "$TMP"
echo "$PING_RESULT" >> "$TMP"
echo "$PING_RESULT2" >> "$TMP"
echo `date`": tracing connection to $SERVER (via traceroute)..." >> "$TMP"
TRACE=`$TRACEROUTE -m 10 "$SERVER" 2>&1` # trace problem
TRACE_HOSTS=`echo "$TRACE" | grep -v \* | wc -l` # good hosts
echo "$TRACE" >> "$TMP" # show trace
if [ $TRACE_HOSTS -gt 3 ] ; then # 3 good hosts?
echo `date`": failure at or near $SERVER" >> "$TMP"
# 4 or more hops? somewhere on the internet or at test server
elif [ $TRACE_HOSTS -gt 2 ] ; then # 2 good hosts?
echo `date`": FAILURE AT INTERNET PROVIDER" >> "$TMP"
# 3 hops? you to internet provider's provider
elif [ $TRACE_HOSTS -gt 1 ] ; then # 1 good host?
echo `date`": FAILURE AT INTERNET PROVIDER" >> "$TMP"
# 2 hops? you to internet provider
else # should not happen
echo `date`": UNABLE TO REACH INTERNET PROVIDER" >> "$TMP"
# 1 hop? didn't get to first computer after this one. how did
# we log in?
fi
elif [ -z "$OK" ] ; then # ping good?
echo `date`": OK" >> "$TMP" # we're OK
OK=1
fi
elif [ -z "$OK" ] ; then # ping good?
echo `date`": OK" >> "$TMP" # we're OK
OK=1
fi
fi
fi
# Cleanup
if test -s "$TMP" ; then # messages?
cat "$TMP" >> "$LOG" # copy to log
rm "$TMP" # remove msgs
fi
rm "$LOCK" # remove lock
sleep 120 # every 2 minutes
done
# Should not get here
exit 0
![[Navigation Bar]](../art/nav_bar_head2.png)