View Full Version : Ping or whatever script to reconnect a server when it loses connectivity
flx-
December 17th, 2008, 13:00
Hi i need help from you guys, im looking for a simple ping or whatever script to keep a server reconnected to a network when it loses leases (when using dhcp) or can not keep connected to the default route. Any ideas? i can not find anything useful on google x(
DutchDaemon
December 17th, 2008, 13:13
What do you need? One ping per minute or so?
#!/bin/sh
while true
do
/sbin/ping -c 1 some.host.com > /dev/null 2>&1
/bin/sleep 60
done
Give the script a name and run it like somescript &, for example from /etc/rc.local (so it will start at boot time).
brd@
December 17th, 2008, 16:55
The DHCP Client should renew the lease before it expires automatically. Please tell us more about how the machine is connected. Does the link go down when this happens?
flx-
December 18th, 2008, 01:18
Well is a server that acts as gateway for my small corporate network, it has a satellital connection with static ip, but from time to time the connection drops and i have to restar the interfaces with /etc/rc.d/netif restart and the routing (/etc/rc.d/routing restart. i need a solution that automatically handle this issue at any time, i though that a ping scrip can do the job, pingin for example to google and then if fail execute the necesary command to keep connected. I hope to be clear because my english its not so well hehe...
bsddaemon
December 18th, 2008, 01:45
Hmm, you should have known how to solve this little problem, because you know the commands:
/etc/rc.d/netif restart
/etc/rc.d/routing restart
Anyway, I would append this line to the /etc/crontab file:
*/5 * * * * root ping -c2 google.com || (cd /etc/rc.d/ && ./netif restart && ./routing restart)
Or you can put those commands into a script, up to you, though
flx-
December 18th, 2008, 02:58
Its ok but i would like to see those commands to be executed only if the ping to google fails, or just can´t find route to host. For that reason it should be on a script, but i dont know how to do it and that´s what i really need. Can you help me with a very simple script using those commands?
cajunman4life
December 18th, 2008, 07:59
The crontab entry posted by bsddaemon will do exactly what you request:
*/5 * * * * root ping -c2 google.com || (cd /etc/rc.d/ && ./netif restart && ./routing restart)
What that does is every 5 minutes, it tries to ping google.com. If the ping fails, it executes everything after the "||" lines, which is to restart netif and routing, which is the step you're presently doing manually. If you want to execute this in a script, then put the following:
#!/bin/sh
ping -c2 google.com || (cd /etc/rc.d/ && ./netif restart && ./routing restart)
Into a file called "netcheck.sh" (or whatever name you so choose), make it executable (chmod +x netcheck.sh) and then run it using:
./netcheck.sh &
as root.
The simplest one would be to copy and paste the above crontab entry to /etc/crontab.
flx-
December 18th, 2008, 12:33
Well thanks let me try that :D i hope it works hehe because im tired of doing the same thing every time at work :P
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.