Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to write a simple script that can be executed as a cron job. All that I need for it to do is ping a few addresses on the same lan segment and if any of the pings fail then reboot the machine that the script is on. Im sure this is a simple if/then statement but I am not a programmer. Does anybody know the code to make this happen?
You can type in "help if" in the console, since it is a built-in command. You can also look in the "info bashref" manual for more information. There is also a very good book on the web at the Linux Documenation Project website on "Advanced Bash Scripting" http://www.tldp.org/LDP/abs/abs-guide.pdf
However, I don't understand why you would want to reboot the machine. If one of the pings fails, it is probably due to the machine you are trying to ping. The computer running the script is working and the network interface is working, unless all of pings fail.
If all of the pings fail, it would be better to restart the interface instead of rebooting.
I have a series of linksys wrt54g routers with the sveasoft firmware applied and this is why the script is needed. I have the routers setup in a mesh network topology and the main router/gateway router talks to the routers that are in WDS (repeater mode) with no problems after a reboot but if a few minutes go by and then I try to ping the repeater mode routers I seem to have lost communication until the gateway router is rebooted again. I need to automate the ping/reboot issue to keep my client pcs happy until I can diagnose the issue.
Is it a wrt54g that is running the cron script?
Does this firmware add features, or is it a mini linux distro?
Is the main router/gateway another wrt54g?
If it is possible to restart the network interface that would be a better choice.
I'm not at my linux machine right now, so I had to check out the ping command for cygwin. If the ping is successful, it returns 0. So you can something like "if [ ping <host> -c 1 ]; then <restart interface or reboot gateway>; fi"
I still think that something along the lines of "service network status <interface>" may be a better choice. An ifplugd script may be even better than using ping. It monitors a heatbeat signal over the interface. If the connection is dropped, it runs an action script.
All of the routers are WRT54Gs, including the gateway router. It is a firmware that adds features and does have cron ability. It also has a RW partition for adding startup and firewall scripts. There is already a heartbeat feature but I am not sure how it works as far as getting it to run scripts.
yeah, if the machine you're pinging fails(I mean the net connection, not just ICMP), how can you reboot it? the scripts still need network connections right?
iplist='192.168.1.100 192.168.1.101 192.168.1.102'
failed=0
for ip in "${iplist}"; do
if ! ping ${ip}; then failed=1
done
if [ failed==1 ]; then $reboot; fi
In place of $reboot, you would put your restart or reboot command. Perhaps, "/sbin/shutdown -r now".
Here is a nice little trick. If you have a command defined in a variable:
command='/sbin/shutdown -r now'
you can run it using the variable:
if [ failed==1 ]; then $command; fi
will run the shutdown command.
Well, the plot thickens. It seems that cron is broken on the sveasoft talisman firmware and I have been unsuccessfull in getting cron to execute ANY scripts.
I have a CentOS box on the same lan segment as the router in question and I have ssh enabled on the router so my question now is how can I have the ping/reboot script sit on my CentOS box and if the ping result is fail then have the script execute a remote restart of the router from the CentOS box?
centos hosts ping script executed every 15 min by cron ---> script pings router addresses on local net ----> pings successfull (do nothing) ----> pings fail then execute a remote restart on router1
I can't help with the commands necessary to reboot the remote device, but the following shell template should help get you started in the right direction.
Code:
[root@excelsior test]# cat foo
#!/bin/bash
# Variable for IP's to ping
HOSTS_TO_PING="
192.168.9.4
192.168.9.5
"
# Define all shell functions
reboot_device ()
{
IP_HOSTNAME=$1
echo "rebooting device ${IP_HOSTNAME}"
# add code to reboot remote device
}
# Start of executable code
for IP in ${HOSTS_TO_PING} ; do
ping -c1 ${IP} >/dev/null 2>&1 || reboot_device ${IP}
done
This is indeed better in a cron script.
ping -c1 ${IP} >/dev/null 2>&1
I forgot about redirecting the output of the ping, since a cron job runs in the background.
That could be why your cron job didn't run properly.
In my sample, if the first host fails, the others are pinged unnecessarily. In Scowles code, if the first host fails, the router is rebooted, but the pinging continues even though the router is resetting. Due to the resulting timeouts, the router might be rebooted more than once.
You might use the 'break' command to break out of a loop if a ping fails.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.