1826446 Members
4192 Online
109692 Solutions
New Discussion

Ping Script

 
SOLVED
Go to solution
Nikee Reddy
Regular Advisor

Ping Script

Hello,

I have a list of IP addresses. All I want is to ping the each and every IP address from the list/file and should able to send me an SMS message/email in case of ping failure.

Could you please let me know the script for this.

"ping -n 4"

Thanks,
Nikee
4 REPLIES 4
Biswajit Tripathy
Honored Contributor

Re: Ping Script

The script could look something like this:

IP_LIST="192.168.0.1 192.168.0.2 192.168.0.3"
for ip in $IP_LIST
do
ping $ip -n 1 | grep -q "100% packet loss"
if [ $? -eq 0 ]
then
mailx -s "Can't ping $ip" mail@company.com
fi
done

Ofcourse, you need to test the above script and
check for syntax errors as I did not run it on an
actual machine. Note that you could use hostname
instead of IP address in the IP_LIST, but make
sure that nslookup on the hostname returns
success; otherwise the above script will return
ping success for non-existing hostnames. Handling
non-existing hostname should be the enhancement
to this script.

- Biswajit
:-)
J Busch
Advisor
Solution

Re: Ping Script

I would look at a utility called fping.

Far more flexible then just ping.
Software can be found here http://hpux.cs.utah.edu/

Something we use to check routers, switches and servers.

Basic script
/opt/fping/sbin/fping -u -f {file of hosts} > /tmp/ping_errors

VAR=`cat /tmp/ping_errors |wc -l`
if [ $VAR -gt 1 ]
then
mailx -s "ping Alert" pager@myairmail.com < /tmp/ping_errors

else
echo "Everything on line" > /tmp/ping_errors
Stanimir
Trusted Contributor

Re: Ping Script

Hi,

I have attached you a small example, using Net:Ping
and two hosts to ping.


Regards,Stan
Ralph Grothe
Honored Contributor

Re: Ping Script

Hi Nikee,

the Net::Ping module that Stanimir mentioned is well worth noting.
Other than scripts that use the system ping command that use ICMP packages and thus require root privileges (have a look at the setuid bit of /usr/sbin/ping) the Net::Ping is much more flexible.
Therefore I would rather chose TCP or UDP (the default of net::Ping) for transport.
Just specify the transport as an argument in the constructor call.
We for instance have many hosts in firewalled LANs, and the firewalls silently drop everything that isn't 22/tcp (and other required services like Domain, NTP etc.)
So there's never an echo reply on an ICMP echo request.
With Net::Ping you can specify the SSH port 22 and chose TCP as transport, and can "ping" all those hosts (together with a control that an sshd is responding.
If you require more sophistication to "Fool" your firewalls then I'd suggest to use nmap
(http://www.insecure.org/nmap/)
It lets you chose all sorts of handshakes or only send SYN packets.
See its manpage for details.
Madness, thy name is system administration