Newbie needs help with ping script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Newbie needs help with ping script
# 1  
Old 11-02-2010
Newbie needs help with ping script

Hi all,

I've been trying to get this script right for the past few days and just can't figure out where I'm going wrong. Since one of my internet connections is extremely flaky I want to run this script to ping every 5 minutes and then reboot the router via telnet if I don't get a response.

Here's the script currently. Notice I've commented out the script and replaced it with an echo. This is because the script was rebooting the router every 5 minutes regardless of if the ping returned a result or not. Please note I'm running this on cygwin (hence the slightly different ping command). Unfortunately that's the only choice I have since the hardware is rather limited and I can't run a VM with linux on it. However I'm also testing it on a remote debian shell and it does the same thing.

Code:
#!/bin/sh

count=$(ping ii.net 56 3 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ "$count" -eq 0 ]; then ECHO "bsasdasd"
#  ./reboot2.sh
fi

reboot2.sh works just fine. It logs into the router and reboots. The ping returns a 0 when I've taken the connection down and returns 3 when it's up. I just can't figure out why the script ignores that and reboots the router anyway.

I've tried both;
Code:
if [ "$count" -eq 0 ];

This returned a "unary code expected" error. Changing it to;
Code:
if [ ${count:-0} -eq 0 ] || [ -z "$count" ]; then

gets rid of the error but the script will still just reboot the router.

I'm sure there's something simple wrong here but I'm new to all this and I've been learning as I go. Any help would be appreciated.
# 2  
Old 11-03-2010
My ping is slightly different (requires -w for timeout and -n for count):

Code:
$ ping ii.net -w 56 -n 3
Pinging ii.net [203.0.178.114] with 32 bytes of data:
Reply from 203.0.178.114: bytes=32 time=72ms TTL=243
Reply from 203.0.178.114: bytes=32 time=72ms TTL=243
Reply from 203.0.178.114: bytes=32 time=73ms TTL=243
Ping statistics for 203.0.178.114:
    Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 72ms, Maximum = 73ms, Average = 72ms

This gives me the Received amount in count:

Code:
$ count=$(ping ii.net -w 56 -n 3 | grep -i 'received' | awk -F',' '{ print $2 }' | awk '{ print $3 }')
$ echo $count
3

and this test seems to do the trick:

Code:
$ if [ "$count" -ne 3 ] ; then ECHO "fjdkfjk" ; fi
$ if [ "$count" -eq 3 ] ; then ECHO "fjdkfjk" ; fi
fjdkfjk

Note here that tcpip is a little flakey and 1 lost packet will cause you to reboot, perhaps you should ping 10 times and ensure at least 7 get thru.
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 11-03-2010
Nothing looks funny on the surface.

If I were debugging the problem, I'd add a set -x to the top of the script to see what actually was being evaluated in the if. Is it possible that there is more than one line coming back from the ping with the word 'received' in it? Don't have a cygwin box, so I cannot test the exact ping to see what the output is; something like that could really cause issues.

@Chubler_XL -- this is really a nit, but ping uses icmp and not TCP.
This User Gave Thanks to agama For This Post:
# 4  
Old 11-03-2010
Thanks guys. That seems to have done it! The script works perfectly when I run it manually however the cron job returns "integer expression expected" for both of the if [ "$count" lines. Not sure why it would do that. Some bug in cygwin's cron maybe?
# 5  
Old 11-03-2010
echo $count before going for the if condition. Guess the variable's value is not numeric hence you get that error.
Also as suggested by agama there are possibilities of getting more than one -word 'received' from grep which maybe the reason for this error.

Last edited by michaelrozar17; 11-03-2010 at 04:10 AM.. Reason: typo..
This User Gave Thanks to michaelrozar17 For This Post:
# 6  
Old 11-03-2010
Huge thanks to everyone who helped out. It's working now. Turns out I needed to put the full path to ping in there instead of just the command, so ping wasn't running and there was no numerical output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Ping test sends mail when ping fails

help with bash script! im am working on this script to make sure my server will stay online, so i made this script.. HOSTS="192.168.138.155" COUNT=4 pingtest(){ for myhost in "$@" do ping -c "$COUNT" "$myhost" &&return 1 done return 0 } if pingtest $HOSTS #100% failed... (4 Replies)
Discussion started by: mort3924
4 Replies

2. Shell Programming and Scripting

Script with ping

I have a question is there any posibility for writing a script that you can see if a pc in your network is up of down , when you run this script for ex. ./test.sh 63 45 54 , which are the ip adresses of the computers , when you give the last digit of the ip adres as argument 63 , 45 and 54 are... (5 Replies)
Discussion started by: Roggy
5 Replies

3. Shell Programming and Scripting

ping script

hello fellows, I need help with a script, I'm using this one HOSTS="192.168.10.9 192.168.10.15 " SUBJECT="Attention - Gateway San Martin DOWN " COUNT=1 EMAILID="lmail@mydomain.com" for myHost in $HOSTS do count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2... (2 Replies)
Discussion started by: lucas.fradusco
2 Replies

4. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

5. Shell Programming and Scripting

Using ping in script

Hi. I have a server with multiple network ports that need to be tested to a list of destinations. I'm trying to write a scripts to automate this but can't seem to get past an error and could use some help. I have two test files one contains the ip addresses of the onboard NICs and the other... (10 Replies)
Discussion started by: mikez104
10 Replies

6. Shell Programming and Scripting

Animation Ping on Solaris Like Cisco Ping

Hi, I develop simple animation ping script on Solaris Platform. It is like Cisco ping. Examples and source code are below. bash-3.00$ gokcell 152.155.180.8 30 Sending 30 Ping Packets to 152.155.180.8 !!!!!!!!!!!!!.!!!!!!!!!!!!!!!. % 93.33 success... % 6.66 packet loss...... (1 Reply)
Discussion started by: gokcell
1 Replies

7. Shell Programming and Scripting

Help With Ping Script

Hello all...I'm new to Unix and learning. What I'm trying to create is a script that will ping a known range of IP addresses, say 192.168.1.1 to 192.168.1.254. For each address that no reply is received, that address will be written to a log file that will be emailed to an administrator. My wife... (1 Reply)
Discussion started by: spmitchell
1 Replies

8. Shell Programming and Scripting

Need a Simple ping script

Hi, I am a learner in shell scripting. Can someone help me in getting a script to run the ping command in the background through a script to get the status of my servers and email me if ping failed with list of servers in one email. Thanks (4 Replies)
Discussion started by: bachusunil
4 Replies

9. Shell Programming and Scripting

Ping script

I want find from a file , where list of hosts is given . Whether SSH is running on that Hosts and and create 2 files : HOSt-SSH-Running HOST-SSH-Not-running In the list we have some Windows and Unix Servers, windows servers do not have sshd running. (2 Replies)
Discussion started by: sriram003
2 Replies

10. Shell Programming and Scripting

Ping Script

hello, I am looking to make a script that will ping a remote ip address. Upon completion of the ping I want the program to either ping again if the # of packets transmitted is equal to the number of packets received or exit if the two values are unequal and information was lost. I am not sure if I... (3 Replies)
Discussion started by: mcrosby
3 Replies
Login or Register to Ask a Question