# Ping or whatever script to reconnect a server when it loses connectivity



## flx- (Dec 17, 2008)

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 (Dec 17, 2008)

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 [font="System"]somescript &[/font], for example from /etc/rc.local (so it will start at boot time).


----------



## brd@ (Dec 17, 2008)

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- (Dec 18, 2008)

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 (Dec 18, 2008)

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- (Dec 18, 2008)

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 (Dec 18, 2008)

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- (Dec 18, 2008)

Well thanks let me try that  i hope it works hehe because im tired of doing the same thing every time at work


----------

