Reputation: 43
I have the following script where I am trying to differentiate between a server that is down and a server that is no longer on the network. If I use the ping command on the command line on a server that is just down and echo the $? I get a 1 as expected. If I use the ping command on the command line on a server that is no longer on the network and echo the $? I get a 2 as expected. I can't seem to capture this behavior in my script. On the script below, the server that is no longer on the network does not appear at all in the badhosts output file. I am using the dev null on the ping line as I don't want to get the host unknown lines on the output which will skew the results.
Thanks in advance for any assistance.
#!/bin/ksh
# Take a list of hostnames and ping them; write any failures
#set -x
for x in `cat hosts`
do
ping -q -c 1 $x > /dev/null 2> /dev/null
if [ "$?" -eq 1 ];then
echo $x is on network but down >> badhosts
elif [ "$?" -eq 2 ];then
echo $x is not on the network >> badhosts
fi
done
Upvotes: 1
Views: 190
Reputation: 43
I modified my script at the suggestion of Raman as follows and this works. Thanks Raman !!
#!/bin/ksh
# Take a list of hostnames and ping them; write any failures
set -x
for x in `cat hosts`
do
ping -c 1 $x > /dev/null 2> /dev/null
pingerr=$?
if [ $pingerr -eq 1 ]; then
echo $x is on network but down >> badhosts
fi
if [ $pingerr -eq 2 ]; then
echo $x is not on the network >> badhosts
fi
done
Upvotes: 1