1752801 Members
5678 Online
108789 Solutions
New Discussion юеВ

shell scriptf for ping

 
SOLVED
Go to solution
Shivkumar
Super Advisor

shell scriptf for ping

Dear Sirs;

I need to know shell script which performs ping to couple of servers every 15 minutes and records the output in a file.

Thanks,
Shiv
12 REPLIES 12
VEL_1
Valued Contributor

Re: shell scriptf for ping

Refer the following simple script


#!/sbin/sh
i=1
while [ $i -eq 1 ]
do
for str in $hosts
do
echo "" >>output_file
ping $str >>output_file 2>&1
if [ $? -ne 0 ]
then
echo "There is problem while nslookup $str" >>output_file 2>&1
i=0
fi
done
sleep 900
done
VEL_1
Valued Contributor

Re: shell scriptf for ping


Sorry. Ignore my previous reply.


#!/sbin/sh

hosts="abc.xx.com xyz.ab.com"

i=1
while [ $i -eq 1 ]
do
for str in $hosts
do
echo "" >>output_file
ping $str >>output_file 2>&1
if [ $? -ne 0 ]
then
echo "There is problem while nslookup $str" >>output_file 2>&1
i=0
fi
done
sleep 900
done
Muthukumar_5
Honored Contributor
Solution

Re: shell scriptf for ping

#!/bin/ksh
# Input
server1=
server2=
stime=900

while [ 1 ]
do

date >> /tmp/ping_$server1.log
ping $server1 -n 1 >>/tmp/ping_$server1.log
[[ $? -eq 0 ]] && echo "Ping success " || echo "Ping failure" >>/tmp/ping_$server1.log
date >> /tmp/ping_$server2.log
ping $server2 -n 1 >>/tmp/ping_$server2.log
[[ $? -eq 0 ]] && echo "Ping success" || echo "Ping failure" >>/tmp/ping_$server2.log

sleep 900

done

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: shell scriptf for ping

Raj,

You try of,

ping $str >>output_file 2>&1

will not end ;). Use count with ping.

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: shell scriptf for ping

Shiv,

If you are going to setup these scripts in productive environment plz use cron + scripting as,


#!/bin/ksh
# Input - Must
server1=
server2=
stime=900

date >> /tmp/ping_$server1.log
ping $server1 -n 1 >>/tmp/ping_$server1.log
[[ $? -eq 0 ]] && echo "Ping success " || echo "Ping failure" >>/tmp/ping_$server1.log 2>&1
date >> /tmp/ping_$server2.log
ping $server2 -n 1 >>/tmp/ping_$server2.log
[[ $? -eq 0 ]] && echo "Ping success" || echo "Ping failure" >>/tmp/ping_$server2.log 2>&1

# end
exit 0

# Cron Tab Setting
0,15,30,45 * * * * /tmp/ping.ksh 1>/dev/null 2>&1
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: shell scriptf for ping

No need to use stime as you can use sleep 900 in your script.. Crisp & Concise ;)

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
VEL_1
Valued Contributor

Re: shell scriptf for ping


Sorry. Thought of solaris platform.


Geoff Wild
Honored Contributor

Re: shell scriptf for ping

Sounds like you are doing some monitoring....why not purchase OVO?

If you can't afford it, have a look at Big Brother:

http://www.bb4.org/

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Rajesh SB
Esteemed Contributor

Re: shell scriptf for ping

Hi,

This is my script use for one project.

#!/usr/bin/ksh
> /home/rajeshsb/sysstat.log
> /home/rajeshsb/downlist
for sys in `cat /home/rajeshsb/sys.list`
do
/etc/ping $sys -n 2 |grep ms >> /home/rajeshsb/sysstat.log
if [ $? -ne 0 ]
then
echo "$sys is down" >> /home/rajeshsb/downlist;
else
echo "$sys is up" > /dev/null;
fi
done
if [ -s /home/rajeshsb/downlist ]
then
cat /home/rajeshsb/downlist| mailx -s "Warning! Down System details" rajeshsb@india.hp.com
mailx -s "Warning! Down System details" rajeshsb@india.hp.com < /home/rajeshsb/downlist
fi

Usage:
The script "sysstat.sh" reads the list of systems from file "/home/rajeshsb/sys.list"
It logs as well sends the email to me
Keep on add the systems into sys.list.

Just schedule the scritp in cron.


Regards,
Rajesh