Eksplanation:
Code:
/bin/ping -c1 google.com >/dev/null 2>&1
Ping host to see if it's up
Code:
while [ $? -ge 1 ]; do
while return value is 1 or grater, return value of a success here is 0, any failuer will result in 1(internal error in ping command) 2(unknown host, usualy no link) 3(no route to host), can't remember if theres more
Code:
/sbin/ifdown eth0 >/dev/null 2>&1
/sbin/ifup eth0 >/dev/null 2>&1
sleep 3
bring interface down, set it up again and wayt a few seconds be for initiating that crusial
Code:
/bin/ping -c1 google.com >/dev/null 2>&1
to ping host and have the return value once more holding that condition value the while loop is controled by.
The -c1 is to tell ping to only send one package, so if for some reason where the link is up it dost return, it will reload the network, but thats a question on patience for script runtime and failproof issue.
It can easily be altered to like run this once every 5 minuts or so, just embrace the initial ping and the while loop in a while loop which does nothing more than run forever and sleep any time you like, something like:
Code:
#!/bin/sh
if ! [ -x /sbin/ifdown ]; then
exit 0
fi
if ! [ -x /sbin/ifup ]; then
exit 0
fi
if ! [ -x /bin/ping ]; then
exit 0
fi
while [ 2 -le 3 ]; do
/bin/ping -c1 google.com >/dev/null 2>&1
while [ $? -ge 1 ]; do
/sbin/ifdown eth0 >/dev/null 2>&1
/sbin/ifup eth0 >/dev/null 2>&1
sleep 3
/bin/ping -c1 google.com >/dev/null 2>&1
done
sleep 300
done
This will run for ever and test the connection every 5 minuts.
Sorry that I havn't been around much in tha last 2 years or so.. Yet I've been around just didn't participated in any answering questions here.. Since I didn't find that much of interrest, altho this specific question caught my attention.
Bookmarks