testing ping response


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting testing ping response
# 8  
Old 08-16-2008
ping_stat=`ping -c 4 $2 | grep loss | awk '{print $7}' |cut -d % -f1`
ping will take $2 there is no problem i guess please paste what error you are getting??
# 9  
Old 08-16-2008
vidyadhar85:
Hmm... so, in my case, you suggest i should use something like
Code:
if (( test $ping_stat -gt "25" ))

?

Just for now though, it seems like
Code:
if (( $ping stat < 25 ))

works like i want it to do...

Whats the difference between the two given examples?
# 10  
Old 08-16-2008
Quote:
Originally Posted by vidyadhar85
ping_stat=`ping -c 4 $2 | grep loss | awk '{print $7}' |cut -d % -f1`
ping will take $2 there is no problem i guess please paste what error you are getting??
Lets begin with the full source:
Code:
#! /usr/local/bin/bash

pinger ()
{
ping_stat=`ping -c 4 host.name | grep loss | awk '{print $7}' | sed 's/%//g'`
echo "Packet loss reported = $ping_stat"
test_
}

test_ ()
{
if (( $ping_stat > 25 ))
        then
                if (( $ping_count < 2 ))
                        then
                                let ping_count=ping_count+1
                                echo "More than 25% packet loss! I will take a nap in 2 minutes and then try again before i determine if host is down!"
                                sleep 120
                                echo "Trying again! Ping_count is $ping_count."
                                pinger
                        else
                                echo "The host $2 does not respond to ping. Please investigate ASAP!"
                                touch /tmp/no.notify.beacon
                                looper
                fi
        else
                echo "Host is responding!"
fi

}

looper ()
{
ping_stat_loop=`ping -c 4 host.name | grep loss | awk '{print $7}' | sed 's/%//g'`
echo "Packet loss reported = $ping_stat"
if (( $ping_stat_loop > 75 ))
        then
                echo "Host still down. Trying again in 60 seconds..."
                sleep 60
                looper
        else
                echo "0% packet loss! Host is alive!"
                rm -rf /tmp/no.notify.beacon
fi
}

for _switch ; do
        case $_switch in
        -remote)
                if [ -e /tmp/no.notify ]
                        then
                                exit 0
                        else
                                ping_count=1
                                pinger ;;
                fi
        -boot)
                sleep 90
                get_ip=`ifconfig em0 | grep inet | awk '{print $2'}`
                get_hostname=`uname -n`
                message="$get_hostname has just (re)booted. (new) IP is $get_ip"
                sms=`echo $message | sed 's/ /+/g'`
                echo $message > /tmp/ip_mail.txt
                mail -s "$get_hostname" mail@he.re < /tmp/ip_mail.txt
                rm -rf /tmp/ip_mail.txt
                echo $sms
                ;;
        esac
done

exit 0

And, if i change host.name to $2, this is what i get...
Code:
./pingtest.sh -remote host.name

usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize]
            [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl]
            [-P policy] [-p pattern] [-S src_addr] [-s packetsize] [-t timeout]
            [-W waittime] [-z tos] host
       ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]
            [-M mask | time] [-m ttl] [-P policy] [-p pattern] [-S src_addr]
            [-s packetsize] [-T ttl] [-t timeout] [-W waittime]
            [-z tos] mcast-group
Packet loss reported =
./pingtest.sh: line 12: ((: > 25 : syntax error: operand expected (error token is "> 25 ")
Host is responding!

# 11  
Old 08-16-2008
i mean to say "(("=test="[" these three are equal you can use any of these with if..
and try to run your script sh -x ./scriptname -remote host.name you will get an idea where exactly you are going wrong Smilie
# 12  
Old 08-16-2008
Hmm.. i did that, and still I have no clue why using host as argument doesn't work...
Here the output:

Code:
sh -x pingtest.sh -remote host.name 
+ [ -e /tmp/no.notify ]
+ ping_count=1
+ pinger
+ ping -c 4
usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize]
            [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl]
            [-P policy] [-p pattern] [-S src_addr] [-s packetsize] [-t timeout]
            [-W waittime] [-z tos] host
       ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]
            [-M mask | time] [-m ttl] [-P policy] [-p pattern] [-S src_addr]
            [-s packetsize] [-T ttl] [-t timeout] [-W waittime]
            [-z tos] mcast-group
+ grep loss
+ awk {print $7}
+ sed s/%//g
+ ping_stat=
+ echo Packet loss reported =
Packet loss reported =
+ test_
+
+ 1
1: not found
+ echo The host  does not respond to ping. Please investigate ASAP!
The host  does not respond to ping. Please investigate ASAP!
+ touch /tmp/no.notify.beacon
+ looper
+ ping -c 4
usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize]
            [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl]
            [-P policy] [-p pattern] [-S src_addr] [-s packetsize] [-t timeout]
            [-W waittime] [-z tos] host
       ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]
            [-M mask | time] [-m ttl] [-P policy] [-p pattern] [-S src_addr]
            [-s packetsize] [-T ttl] [-t timeout] [-W waittime]
            [-z tos] mcast-group
+ grep loss
+ awk {print $7}
+ sed s/%//g
+ ping_stat_loop=
+ echo Packet loss reported =
Packet loss reported =
+
+ echo Host still down. Trying again in 60 seconds...
Host still down. Trying again in 60 seconds...
+ sleep 60

I stopped the script with ctrl+c, since it would otherwise get stuck in an infinite loop.

Damn, this is really getting annoying...
Just to test it, inside the case, in the -remote part, i added:
echo $2
exit 0
before setting the ping_count..
And guess what, it echoed the hostname as it should.. so why doesn't $2 work in the rest of the code? *argh*
# 13  
Old 08-16-2008
In the remote section of the case, try calling pinger as
Code:
pinger $2 ;;

and then in pinger,
Code:
 
ping_stat=`ping -c 4 $1 | grep loss | awk '{print $7}' | sed 's/%//g'`

# 14  
Old 08-16-2008
oh man got your problem...
in your script ping is inside the pinger() function right are you passing any arguments to it?? no right??
so where ever you call pinger type pinger $2
hope it works...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 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. Red Hat

Response Times

Hello all. Let me qualify my question by saying that I am struggling with how to ask the question I am semi green but have no issue reading up if pointed in the right direction. Please be gentle! A RHEL server 6.2. Hosts a statistical application that has some web apps and batch programming... (0 Replies)
Discussion started by: rsheikh01
0 Replies

3. Shell Programming and Scripting

Ping Response from the host name

Hi All, I have the requirement where am pinging the server and matching the IP address with the existing IP address. Below code is returning me the IP address and my requirement is i have to see that also whether it is pinging or not PING useipapd01 (172.22.32.87) 56(84) bytes of data. 64... (1 Reply)
Discussion started by: sharsour
1 Replies

4. Shell Programming and Scripting

How to get reason for ping failure using perls Net::Ping->new("icmp");?

Hi I am using perl to ping a list of nodes - with script below : $p = Net::Ping->new("icmp"); if ($p->ping($host,1)){ print "$host is alive.\n"; } else { print "$host is unreacheable.\n"; } $p->close();... (4 Replies)
Discussion started by: tavanagh
4 Replies

5. UNIX for Dummies Questions & Answers

Ping + timestamp + selected response

Hello All, i would like to start ping command and the result should contain also Timestate. this i'm able to do with following command : ping HOSTNAME | perl -nle 'print scalar(localtime), " ", $_' or ping HOSTNAME | awk '/time\=(+\.{2}) ms /^+ bytes from / { "date" | getline pong;... (1 Reply)
Discussion started by: ob3l1x
1 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. AIX

Ping response

:confused:Hi, In linux if ping to a system from a linux server it shows ping time=0.120ms how we can achive this in aix. i need this for a latency check. Thanks in advance. (5 Replies)
Discussion started by: vjm
5 Replies

8. UNIX for Dummies Questions & Answers

response of a for loop?!

Dear guys, I don't know the response of a for loop in this situation: suppose that file1 is an empty file. and i make a for loop as : for i in `cat file1` What will be the response of the for loop: 1- will an error message apear 2- or the for loop simply will not run,and it will escape... (2 Replies)
Discussion started by: marwan
2 Replies

9. SCO

Slow cd response

Hi All We have one SCO Server here and it never gives us any trouble. Until Now!! Well its not earth shattering but we have one user who is complaining of a very slow response time when changing to his Home Directory. Other users who have similar profiles are OK. I have su'd to this user and I can... (0 Replies)
Discussion started by: JohnOB
0 Replies
Login or Register to Ask a Question