LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 04-28-2006, 04:12 PM   #1
vbsaltydog
Member
 
Registered: Nov 2005
Distribution: CentOS
Posts: 154

Rep: Reputation: 15
simple shell script help needed


Hey everyone,

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?

Any help is appreciated,

-vbsaltydog
 
Old 04-28-2006, 05:31 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 04-28-2006, 05:38 PM   #3
vbsaltydog
Member
 
Registered: Nov 2005
Distribution: CentOS
Posts: 154

Original Poster
Rep: Reputation: 15
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.
 
Old 04-28-2006, 06:55 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 04-28-2006, 09:04 PM   #5
vbsaltydog
Member
 
Registered: Nov 2005
Distribution: CentOS
Posts: 154

Original Poster
Rep: Reputation: 15
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.
 
Old 04-28-2006, 09:53 PM   #6
JrLz
Member
 
Registered: Mar 2004
Location: Jakarta
Posts: 164

Rep: Reputation: 30
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?
 
Old 04-29-2006, 08:09 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
you could use something like this is a script:
Code:
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.
 
Old 04-29-2006, 12:49 PM   #8
vbsaltydog
Member
 
Registered: Nov 2005
Distribution: CentOS
Posts: 154

Original Poster
Rep: Reputation: 15
Thank you. This looks like what I need. I will give it a go and report the results.

-vbsaltydog
 
Old 04-29-2006, 10:32 PM   #9
vbsaltydog
Member
 
Registered: Nov 2005
Distribution: CentOS
Posts: 154

Original Poster
Rep: Reputation: 15
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



Any ideas are appreciated,

-vbsaltydog
 
Old 04-30-2006, 08:12 AM   #10
scowles
Member
 
Registered: Sep 2004
Location: Texas, USA
Distribution: Fedora
Posts: 620

Rep: Reputation: 31
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
 
Old 05-02-2006, 01:47 AM   #11
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 05-02-2006 at 02:09 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple script needed drum2jc Linux - Software 1 01-05-2006 12:28 AM
Simple Shell Script Help Kristijan Programming 3 06-13-2005 09:13 PM
simple backup script needed ferret_dude Programming 4 05-24-2004 08:45 AM
a simple shell script Warchief Programming 1 07-31-2003 05:01 AM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 12:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration