Mailinglist Archive: opensuse (1540 mails)

< Previous Next >
Re: [opensuse] Script to check interface & restart network service
  • From: "David C. Rankin" <drankinatty@xxxxxxxxxxxxxxxxxx>
  • Date: Tue, 16 Feb 2010 17:09:22 -0600
  • Message-id: <4B7B25A2.8050308@xxxxxxxxxxxxxxxxxx>
On 02/16/2010 04:53 AM, Wong wrote:
On 02/14/2010 08:30 PM, Wong wrote:
<snip>
I know that I should ping my provider router (a.b.c.d) instead yahoo.com
and put the addr into script.

Is the script to be run from external file or put into after.local? Please
advise me which is better.

Thanks a lot!

Wong



Wong,

The following should do what you want based on ifconfig instead of
ping. Enjoy:

#!/bin/bash

# check for root and use sudo if not root
[[ $UID -eq 0 ]] && prefix="" || prefix="/usr/bin/sudo "

runfile=/var/run/chkdsl0.run
tmpfile=/tmp/chkdsl0
ckinterval=${1:-60}
iface=dsl0

## generic cleanup function used by trap
cleanup() {
sudo rm -f $runfile
}

trap cleanup SIGINT SIGTERM EXIT

## test uses ifconfig to test for the presence of 'inet addr:' for dsl0 (or
$iface)
dsl0test() {
declare -a ifacestatus
IFS=$'\n'

ifacestatus=( $(ifconfig) )

for ((i=0;i<${#ifacestatus[@]};i++)); do
testline=${ifacestatus[$i]}
[[ ${testline:0:4} =~ $iface ]] && break
done

linkidx=$((i+=1))

if [[ ${ifacestatus[$linkidx]} =~ 'inet addr:' ]]; then
return 0
else
return 1
fi

}

## if the runfile is still present check the PID to see if it is running
[[ -f $runfile ]] && {
ps ax | grep -q $(<$runfile) &>/dev/null
PIDrunning=$?
}

## if not already running, start checking dsl0 connection
if [[ ! -f $runfile ]] || [[ $PIDrunning != 0 ]]; then
echo "$$" > $tmpfile && ${prefix}cp $tmpfile $runfile && rm $tmpfile
## keep runing until killed or $runfile deleted
while [[ -f $runfile ]] && [[ $$ == $( <$runfile ) ]]; do
## if still OK, sleep for $ckinterval seconds, if dls0 down restart network
if dsl0test; then
sleep $ckinterval
else
${prefix}rcnetwork restart
fi
done
fi

exit 0



--
David C. Rankin, J.D.,P.E.
Rankin Law Firm, PLLC
510 Ochiltree Street
Nacogdoches, Texas 75961
Telephone: (936) 715-9333
Facsimile: (936) 715-9339
www.rankinlawfirm.com
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >