System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

Script for Network Ping Status.

SOLVED
Go to solution
Narendra Uttekar
Regular Advisor

Script for Network Ping Status.

Hi,
I am not good in script writing, Can anyone help me out to write a script for network ping status i.e. i want to ping for 15 servers and if ping replies back with"relpy from server" then i don't want that output to be e-mailed, but if ping replies back with "request timed out" then it should send me and mail for that host unreachable.

Thanks,
Narendra
12 REPLIES
Laurent Menase
Honored Contributor

Re: Script for Network Ping Status.

#!/usr/bin/ksh
# all the system to ping:
ALLSYSTEM="192.168.0.1 " \
"192.168.0.2 " \
"192.168.0.3 "

monmail="myaddress@mystystem"
pingit()
{
ping $1 -n 1 -m 1 >/dev/null
curval=$?
echo "$1 not responding" |mailx -s "system monitoring: $1 status" $2
return $curval
}

for i in $ALLSYSTEM
do
pingit $i $monmail
done
Narendra Uttekar
Regular Advisor

Re: Script for Network Ping Status.

Hi,
I tried ur script getting error as below,
hostname:[/home]# ./pingstatus
./pingstatus[3]: 10.133.12.6: not found
hostname:[/home]# cat pingstatus
#!/usr/bin/ksh
# all the system to ping:
ALLSYSTEM="10.133.12.29" \
"10.133.12.6" \
"10.133.12.26"

monmail="narendra.uttekar@xxxx.com"
pingit()
{
ping $1 -n 1 -m 1 >/dev/null
curval=$?
echo "$1 not responding" |mailx -s "system monitoring: $1 status" $2
return $curval
}

for i in $ALLSYSTEM
do
pingit $i $monmail
done

I also tried with giving space between "10.133.12.6 " \ ,But still result same.

Thanks,
Narendra

Laurent Menase
Honored Contributor

Re: Script for Network Ping Status.

yes indeed
it is ALLSYSTEM="sys1 \
sys2 \
sys3 \
sys4"

or ALLSYSTEM="sys1 sys2 sys3 sys4"

when doing ALLSYSTEM="x " \
"y "
there is a space between "x " and "y "
so it tries to execute "y "

when doing
"x"\
"y"\
"z"
it is like "xyz"
so the 3 possibilities are
"x "\
"y "\
"z "
or
"x \
y \
y"
or "x y z"
Steven E. Protter
Exalted Contributor

Re: Script for Network Ping Status.

Shalom,

A very interesting document on this topic:
http://www.docs.hp.com/en/J5683-90002/J5683-90002.pdf

Also, ping has a timeout feature that you should build into the script to make it more tolerant of temporary network issues.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Narendra Uttekar
Regular Advisor

Re: Script for Network Ping Status.

Hi Laurent,
After modification the script as suggested by you. The script is running but even though i am able to ping the servers. It is sending me the mail "10.133.12.6 not responding"
#!/usr/bin/ksh
# all the system to ping:
ALLSYSTEM="10.133.12.29 10.133.12.6"

monmail="narendra.uttekar@xxxx.com"
pingit()
{
ping $1 -n 1 -m 1 >/dev/null
curval=$?
echo "$1 not responding" |mailx -s "system monitoring: $1 status" $2
return $curval
}

for i in $ALLSYSTEM
do
pingit $i $monmail
done

Steven E. Protter
Exalted Contributor

Re: Script for Network Ping Status.

Shalom,

This thread may help.

http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=103638

http://hpux.connect.org.uk/hppd/hpux/Networking/Misc/ping-99.10/man.html

try adding -t value to your ping.

The default timeout does not give sufficient time for the answer to return to your ping command.

Also, put a set -x in the script and lets get some diagnostic data.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Mel Burslan
Honored Contributor
Solution

Re: Script for Network Ping Status.

syslist="10.133.12.29 10.133.12.6"
notifyemail=narendra.uttekar@xxxx.com
for server in $syslist
do
rand=$RANDOM
ping $server -n 3 -m 1 > /var/tmp/tempfile.$ran} 2>&1
grep "100% packet loss" /var/tmp/tempfile.$ran}
r=${?}
if [ $r -eq 0 ]
then
echo "Server ${server} failed ping test}|mailx -s "Ping Test Status" $notifyemail
fi
rm /var/tmp/tempfile.$ran}
done
________________________________
UNIX because I majored in cryptology...
Mel Burslan
Honored Contributor

Re: Script for Network Ping Status.

sorry .. typo error on lines 6 & 7

ran}

should have been

{ran}

________________________________
UNIX because I majored in cryptology...
Laurent Menase
Honored Contributor

Re: Script for Network Ping Status.

indeed second mistake:

pingit()
{
ping $1 -n 1 -m 1 >/dev/null 2>&1
if [ $? = 0 ]
then
return
fi
echo "$1 not responding" |mailx -s "system monitoring: $1 status" $2
}

-m 1 parameter will put a timeout of 1 second.



James R. Ferguson
Acclaimed Contributor

Re: Script for Network Ping Status.

Hi:

> Mel: I think you mean:

# ran=$RANDOM

...since it's the 'ran' variable you later reference :-)

That said, you can make a unique temporary file with the basename and pid of the process. A trap can be used to automatically remove it too:

MYNAME=${0##*/}
MYLOG=/var/tmp}/${MYNAME}.${$}
trap 'rm -f ${MYLOG}' EXIT

Regards!

...JRF...
Mel Burslan
Honored Contributor

Re: Script for Network Ping Status.

Sorry... JRFs right... I should not try attempting to put together scripts before I consume my daily allowance of caffeeine.

Also another missing curly bracket I noticed here:

rm /var/tmp/tempfile.$ran}

should be

rm /var/tmp/tempfile.${ran}

copy and pastis easyf yowrote the first instance right. Otherwise it cascades the errors.
________________________________
UNIX because I majored in cryptology...
Narendra Uttekar
Regular Advisor

Re: Script for Network Ping Status.

Hi All,
Thanks a lot for the solution.