1834207 Members
2673 Online
110066 Solutions
New Discussion

Re: script question

 
Ragni Singh
Super Advisor

script question

Hey all,

I have this script here that I ran every so often to ping my connections. How do I use the grep statement and have it grep for 100% packetloss and then display just the packet loss info. to the screen. I hope I'm making clear what I'm asking for here. Points will definitely be assigned.

host=$(hostname)
case $host in
"hostname")
echo "PINGING FROM $host"
/etc/ping hostname -n 3
/etc/ping 192.168.22.36 -n 3
/etc/ping 192.168.24.36 -n 3
/etc/ping 192.168.22.34 -n 3
/etc/ping 192.168.24.34 -n 3
echo "PINGING TO POPS"
/etc/ping 192.159.81.20 -n 3
;;
* ) echo "$host not in list"; exit 1;;
esac
exit

4 REPLIES 4
Jean-Luc Oudart
Honored Contributor

Re: script question

I have an answer in a previous thread that may help you.
You may have to adapt for the %
Jean-Luc
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x64a193e260b0d611abdb0090277a778c,00.html
fiat lux
John Poff
Honored Contributor

Re: script question

Hi,

I know you are looking for an answer with grep, but you can do it without grep'ing the result. You can just test the result of the ping in an if statement and go from there. Here is a thread with a similar script:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xfbf35f260cafd4118fef0090279cd0f9,00.html

JP
Vincent Fleming
Honored Contributor

Re: script question

host=$(hostname)
case $host in
"hostname")
echo "PINGING FROM $host"
(/etc/ping hostname -n 3
/etc/ping 192.168.22.36 -n 3
/etc/ping 192.168.24.36 -n 3
/etc/ping 192.168.22.34 -n 3
/etc/ping 192.168.24.34 -n 3
echo "PINGING TO POPS"
/etc/ping 192.159.81.20 -n 3) | grep "100% packet loss"
;;
* ) echo "$host not in list"; exit 1;;
esac
exit
No matter where you go, there you are.
Darrell Allen
Honored Contributor

Re: script question

Here's a version to actually display the packet loss info:

#/usr/bin/sh
#for addr in hostname 192.168.22.36 192.168.24.36 192.168.22.34 192.168.24.34
do
stat=`/usr/sbin/ping $addr -n 3 | grep "100% packet loss"`
if [ $? -eq 0 ]
then
echo $addr: $stat
fi

# Alternate method since I've read that $? may be set by ping on 10.20
# to 0 regardless of whether it succeeds or fails
if [ X"$stat" != X ]
then
echo $addr: $stat
fi
done

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)