Operating System - HP-UX
1830242 Members
2172 Online
109999 Solutions
New Discussion

shell script using PING command

 
SOLVED
Go to solution
vas  bolpali
Advisor

shell script using PING command

I am looking for script
which will read the file(contains IP address only)
and check the each IPaddress responce from the PING command,
and generates/append the file output for each IP address log file
(MONTH_IPADDRESS.XLS).
(MMDDHHMI - means MONTH date hours minutes - )

MONTH_IPADDRESS.XLS
if the PING is successful(0% pocket loss),
then append the follwing data

--------------------
MMDDHHMI 100
---------------------

if the PING is not successful(100% pocket loss),
then append the follwing data

--------------------
MMDDHHMI 0
---------------------



if the date is 1st of the month
the file (MONTH_IPADDRESS.XLS)
should be created , otherwise appended.


$cat ipfile
IPADDRESS1
IPADDRESS2
IPADDRESS3




$cat MM_IPADDRESS1.xls
10281010 100
10281020 100
10281030 100 - 100 is uptime.
10281040 0 - 0 is downtime.
10281050 0
10281100 100
10281110 100
10281115 100
MMDDHHMI 100



(basically I am trying to generate excel file
to show the graph of UPTIME of all the servers)

Thnx in Advance .
VB
keeping you ahead of the learning curve
5 REPLIES 5
Govind
Frequent Advisor

Re: shell script using PING command

Try This!!
#! /bin/sh

host=`cat ipfile`
for item in $host
do
echo "Checking $host..."
ping_status=`ping $host 10|grep 'no answer'`
if [ "ping_status" ]; then
echo "$host NOT RESPONDING"
echo " "
continue
fi
echo "$host UP"


Ofcourse this is going to generate a TEXT file which you can import to Excel. You could also modify the echo commands to suit ur needs.
Cheers
Govind
Dont try to fix something till it Aint Broke...Honesty is not always the best policy.....
A. Clay Stephenson
Acclaimed Contributor

Re: shell script using PING command

I'll give you the key to this puzzle. A perl script called 'ping.pl'.

ping.pl -t 10 remote_host
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "Host ok"
else
echo "Host Bad"
fi

and a timeout of 10 seconds is set.

Invoke it as ping.pl -u for full usage.

The idea is to call the perl script for each host and simply look at the return status - 0 means good. That's much easier than parsing for packet loss and it includes an adjustable timeout.

The rest of the script should be very easy but is left as an exercise for the reader.


Regards, Clay



If it ain't broke, I can fix that.
Darrell Allen
Honored Contributor
Solution

Re: shell script using PING command

Hi,

Normally I don't reply with an entire script but here's one for you.


#!/usr/bin/sh

hostsfile=/tmp/hosts_to_ping

DAY=`date +%d`

cat $hostsfile | while read addr
do
OUTFILE=`date +%b`_$addr.XLS

if [ $DAY = 1 ]
then
>$OUTFILE
fi

/usr/sbin/ping $addr -n 3 | grep -q "100% packet loss"
if [ $? != 0 ]
then
# location is up
PCT=100
else
# location is down
PCT=0
fi
echo `date +%m%d%H%M` $PCT >>$OUTFILE
done


I wouldn't trust the results of 1 single ping (hence -n 3).

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

Re: shell script using PING command

If the true state of the machine is needed, ping is very poor metric. The response occurs at a low level in the kernel so you may get a ping from a badly locked up server. True, if the server is not responding to the network, ping will fail but that may also be a network (switch, router, cable) problem.

What makes more sense is to use ping as a front end, then remsh (remote shell) or ssh to query the server for it's current uptime.


Bill Hassell, sysadmin
Dave Wherry
Esteemed Contributor

Re: shell script using PING command

Adding to Bill's thought. I have not seen them but, have been told there are NIC's that will respond to a ping even if the system is down. You do need something more interactive with the system like checking uptime.