PING broken check

Dear All,

Required a script which will check the PING response from my source file(tmp/PingStatus.txt) containing the hostnames.

If ping response is not responding(i.e when hostname is not alive) then this cmd to be executed :

opcmsg object=PING a=OS msg_grp=win-ping severity=critical msg_text="PING BROKEN on $line"

With regards,
Mjoshi

for i in `cat tmp/PingStatus.txt`
do
ping -c 5 -w 5 -n $i | grep -q "bytes from" && echo "$i is OK" || opcmsg object=PING a=OS msg_grp=win-ping severity=critical msg_text="PING BROKEN on $line"
done

Dear its failing..........

root@ggnems21 # cat testping.sh
#!/bin/sh
for i in `cat tmp/PingStatus.txt`
do
ping -c 5 -w 5 -n $i | grep -q "bytes from" && echo "$i is OK" || opcmsg object=PING a=OS msg_grp=OpC severity=critical msg_text="PING BROKEN on $line"
done
root@ggnems21 # ls -l PingStatus.txt
-rwxrwxrwx   1 root     root           7 Jun 20 17:32 PingStatus.txt
root@ggnems21 # sh testping.sh
cat: cannot open tmp/PingStatus.txt

---------- Post updated at 07:07 AM ---------- Previous update was at 07:06 AM ----------

root@ggnems21 # cat testping.sh
#!/bin/sh
for i in `cat tmp/PingStatus.txt`
do
ping -c 5 -w 5 -n $i | grep -q "bytes from" && echo "$i is OK" || opcmsg object=PING a=OS msg_grp=OpC severity=critical msg_text="PING BROKEN on $line"
done
root@ggnems21 # ls -l PingStatus.txt
-rwxrwxrwx   1 root     root           7 Jun 20 17:32 PingStatus.txt
root@ggnems21 # sh testping.sh
cat: cannot open tmp/PingStatus.txt

use /tmp instead of tmp

#!/bin/sh
for i in `cat /tmp/PingStatus.txt`
do
ping -c 5 -w 5 -n $i | grep -q "bytes from" && echo "$i is OK" || opcmsg object=PING a=OS msg_grp=OpC severity=critical msg_text="PING BROKEN on $line"
done
root@ggnems21 # sh testping.sh
grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .
ping: illegal option -- w
usage: ping host [timeout]
usage: ping -s [-l | U] [adLnRrv] [-A addr_family] [-c traffic_class]
        [-g gateway [-g gateway ...]] [-F flow_label] [-I interval]
        [-i interface] [-P tos] [-p port] [-t ttl] host [data_size] [npackets]

just execute the below command and give the result

ping anyhostname

is it showing like "hostname is alive" ?
or is it keep on pinging ?

root@ggnems21 # ping ggnems01
ggnems01 is alive

I also required it to wait for some time because for some occasions the network is slow and i get delay in response of about 5-10 seconds...

sleep 5 -- waiting for 5 seconds, after that we are pinging the host

#!/bin/sh
for i in `cat /tmp/PingStatus.txt`
do
sleep 5; ping $i | grep -w "alive" && echo "$i is OK" || opcmsg object=PING a=OS msg_grp=OpC severity=critical msg_text="PING BROKEN on $i"
done
1 Like

That's a useless use of cat and useless use of backticks. Whenever you have for x in `cat file` you can do it better as

#!/bin/sh
while read i
do
sleep 5; ping $i | grep -w "alive" && echo "$i is OK" || opcmsg object=PING a=OS msg_grp=OpC severity=critical msg_text="PING BROKEN on $i"
done < /tmp/PingStatus.txt

That way you're not trying to cram entire files into shell variables at once and potentially truncating them.

1 Like

Need to discuss a general query:

root@ggnems21 # ping -s ggnems23
PING ggnems23: 56 data bytes
64 bytes from ggnems23.aircel.co.in (172.18.124.38): icmp_seq=0. time=17.6 ms
64 bytes from ggnems23.aircel.co.in (172.18.124.38): icmp_seq=1. time=38.3 ms
64 bytes from ggnems23.aircel.co.in (172.18.124.38): icmp_seq=2. time=122. ms
64 bytes from ggnems23.aircel.co.in (172.18.124.38): icmp_seq=3. time=235. ms
64 bytes from ggnems23.aircel.co.in (172.18.124.38): icmp_seq=4. time=24.8 ms
^C
----ggnems23 PING Statistics----
5 packets transmitted, 5 packets received, 0% packet loss
round-trip (ms) min/avg/max/stddev = 17.6/87.5/235./92.

when is Ping Broken is valid ? i.e at what packet percentage i should comment it as PING BROKEN?

---------- Post updated at 01:10 AM ---------- Previous update was at 01:07 AM ----------

at what percentage packet loss?
If so then i would require this packet loss percetage to be included within my script

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

@corona

thanks for your tips.

any way, in the while loop, we dont want to put cat

cat /tmp/PingStatus.txt

we can use /tmp/PingStatus.txt itself

correct me, if i am wrong

You are correct. the 'cat' was left in by accident, sorry.

i try some script for your needs :wink:

# cat pinglist
localhost
localhost2
192.168.80.130

you can use this format "./testping.sh counts(packet try) hostlistfile"
and i defined some values in script like ; pingloss of percent(threshold value)=%50 ; send packet try waits=2..
but you change all settings if you think what about its..
for example

# ./testping.sh 1 pinglist
"localhost" is alive with "%0 of %100 loss rate" at 1 packet tries in 2 seconds intervals
 
ping: unknown host localhost2
ping failed while sending 'echo_request(s)' to "localhost2"
check!! host access (/network or firewall or route(ing) settings/) or
check!! resolution (/hosts/dns/nsswitch)!!
or
check!! be sure host is power up or
check!! is ping enabled in o.s or switch(s) or router(s)
 
ping failed while sending 'echo_request(s)' to "192.168.80.130"
check!! host access (/network or firewall or route(ing) settings/) or
check!! resolution (/hosts/dns/nsswitch)!!
or
check!! be sure host is power up or
check!! is ping enabled in o.s or switch(s) or router(s)
# cat testping.sh
## justdoit ## ping test on SUN OS ##
#!/usr/bin/bash
if [ $# != 2 ] ; then echo "usage $0 'packetcount' hostlistfile" ; exit 0 ; fi
sleep=2 ; c=$1 # count for packets
while read -r host ; do
(sleep $sleep;ping -s $host 56 $c >/dev/null) &
pid=$! ; wait $pid
if [ $? -eq 0 ] ; then
PL=$(i=1;while [ $i -le $c ] ; do sleep $sleep;ping -s $host 56 $c ;((i++));done)
echo "$PL"|nawk -F, -v c=$1 -v host=$host -v sleep=$sleep '/packets/{sub("% packet loss","",$3)}{t+=$3}
END{if(t>50*c){print "ping problem in \""host"\" !!\nping loss \"%"t" of %"100*c"\" for at "c" packet tries in "sleep" seconds intervals\n"}
else {print "\""host"\" is alive with \"%"t" of %"100*c" loss rate\" at "c" packet tries in "sleep" seconds intervals\n"}}'
else echo -e "ping failed while sending 'echo_request(s)' to \"$host\"\n\
check!! host access (/network or firewall or route(ing) settings/) or \ncheck!! resolution (/hosts/dns/nsswitch)!!\n\
or \ncheck!! be sure host is power up or \ncheck!! is ping enabled in o.s or switch(s) or router(s)\n"
fi;done<$2

regards
ygemici

1 Like

ygemici,
please also include this statement passed within your script provided whenever i get ping broken
i.e If ping response is not responding(i.e when hostname is not alive) then this cmd to be executed :

opcmsg object=PING a=PING msg_grp=win-ping severity=critical msg_text="PING BROKEN on $line"

#!/usr/bin/bash
if [ $# != 2 ] ; then echo "usage $0 'packetcount' hostlistfile" ; exit 0 ; fi
sleep=2 ; c=$1 # count for packets
while read -r host ; do
(sleep $sleep;ping -s $host 56 $c >/dev/null) &
pid=$! ; wait $pid
if [ $? -eq 0 ] ; then
PL=$(i=1;while [ $i -le $c ] ; do sleep $sleep;ping -s $host 56 $c ;((i++));done)
echo "$PL"|nawk -F, -v c=$1 -v host=$host -v sleep=$sleep '/packets/{sub("% packet loss","",$3)}{t+=$3}
END{if(t>50*c) {system("opcmsg object=PING a=OS msg_grp=OpC severity=critical msg_text=\"PING BROKEN on "host"\"") 
print "ping problem in \""host"\" !!\nping loss \"%"t" of %"100*c"\" for at "c" packet tries in "sleep" seconds intervals\n"}
else {print "\""host"\" is alive with \"%"t" of %"100*c" loss rate\" at "c" packet tries in "sleep" seconds intervals\n"}}'
else echo -e "ping failed while sending 'echo_request(s)' to \"$host\"\n\
check!! host access (/network or firewall or route(ing) settings/) or \ncheck!! resolution (/hosts/dns/nsswitch)!!\n\
or \ncheck!! be sure host is power up or \ncheck!! is ping enabled in o.s or switch(s) or router(s)\n"
fi;done<$2

regards
ygemici

1 Like

root@emsnew39 # sh pingtest.sh 50 hostlistfile.txt
pingtest.sh: syntax error at line 8: `PL=$' unexpected
root@emsnew39 #

use bash dont sh..try like this :b:

# ./pingtest.sh 50 hostlistfile.txt

or

# bash pingtest.sh 50 hostlistfile.txt

regards
ygemici

Dear thanks the script is working now,,But my point is now:

For valid host which are pinging, your script is still running..Does that mean the server is still pinging the source ip continuously?

I tried this way also to cross check:

bash pingtest.sh 50 hostlistfile.txt >/tmp/test.txt

but the /tmp/test.txt file is empty

you are right , ping test takes long time..i modified and defined some defaults(for exa ping test about send 4 echo packet check in script)..
now you can give a ping try count for first parameter and then see the results..(actually 50 try requires more times but if you want to test in long time period then maybe you must specify 50 100 or 200 or more than parameter..)
so in script , ping tries transmit packets to target with 2 seconds intervals and 4 packets
first(prestart control) ping test takes about four seconds(normally about 1 second per packet or under 1 second ,, of course it may vary depending on various factors however) if target is avaliable and must must not be resulotion problem [ default timeout is 20 second ].but we defined packet size then it takes about 10 seconds + 4*1 second (per packet) = about 14 seconds.
lets look belows.

# if you dont have access to host..(some waits target host is non-accessable)

# time ping -s 192.168.56.112 56 4
PING 192.168.56.112: 56 data bytes
----192.168.56.112 PING Statistics----
4 packets transmitted, 0 packets received, 100% packet loss
real    0m13.069s
user    0m0.020s
sys     0m0.043s

# if you have access to host..(no wait target host is accessable)

# time ping -s 192.168.56.111 56 10
PING 192.168.56.111: 56 data bytes
64 bytes from solaris (192.168.56.111): icmp_seq=0. time=1.42 ms
64 bytes from solaris (192.168.56.111): icmp_seq=1. time=0.704 ms
64 bytes from solaris (192.168.56.111): icmp_seq=2. time=0.722 ms
........................
64 bytes from solaris (192.168.56.111): icmp_seq=9. time=0.795 ms
----192.168.56.111 PING Statistics----
10 packets transmitted, 10 packets received, 0% packet loss
round-trip (ms)  min/avg/max/stddev = 0.381/0.8182/1.42/0.260
real    0m9.092s
user    0m0.027s
sys     0m0.051s
 

at the now,you must you can try and use to a specifying parameter with new script..
for example

# ./testping.sh 2 hostlist

this means that you want to try for Number of 4-pack counts[this is defaul in script] * 2 count..

#!/usr/bin/bash
## justdoit ## ping test on SunOS ##
if [ $# != 2 ] ; then echo "usage $0 'packetcount' hostlistfile" ; exit 0 ; fi
if [[ $(echo $1|grep [^0-9]) ]] ; then echo "first parameter '$1' is not digit!!";exit 1;fi
sleep=2 ; c=$1 ; control=4 ; # count for packets
while read -r host ; do
printf "%s" "testing access to $host ...? "
(sleep $sleep;ping -s $host 56 $control > /dev/null 2>&1) &
pid=$! ; wait $pid
if [ $? -ne 0 ] ; then
printf "%s\n" "FAIL!! "
else
>tmppg
printf "%s\n" "SUCCESS "
## pinging try ##
printf "%s" "pinging try "
for((j=0;j<$c;j++)); do printf "%c" ".";sleep $sleep; ping -s $host 56 $control >>tmppg;done;
printf "%s\n" " OK"
nawk -F, -v c=$1 -v host=$host -v sleep=$sleep '/packets/{sub("% packet loss","",$3)}{t+=$3}
END{if(t>50*c) {system("opcmsg object=PING a=OS msg_grp=OpC severity=critical msg_text=\"PING BROKEN on "host"\"");
print "ping problem in \""host"\" !!\nping loss \"%"t" of %"100*c"\" for at "c" packet tries in "sleep" seconds intervals"}
else {print "\""host"\" is alive with \"%"t" of %"100*c" loss rate\" at "c" packet tries in "sleep" seconds intervals"}}' tmppg
fi;echo;done<"$2"

good lucks :b:
regards
ygemici

1 Like