k119
k119

Reputation: 55

perl ping failure

#!/usr/bin/perl
use Net::Ping;
$p = Net::Ping->new();
my $main_ip="$ARGV[0]";

if ($p->ping($main_ip,1)){
    $result=true;
    print "$main_ip is alive \n";
}else{
    print "$main_ip is down \n";
}

I am using above perl script to ping check the server. It worked fine all the cases except for IP 192.168.0.168.

$ perl test.pl 192.168.0.168

192.168.0.168 is down

]$ ping 192.168.0.168

PING 192.168.0.168 (192.168.0.168) 56(84) bytes of data.
64 bytes from 192.168.0.168: icmp_seq=1 ttl=64 time=0.304 ms
64 bytes from 192.168.0.168: icmp_seq=2 ttl=64 time=0.355 ms
64 bytes from 192.168.0.168: icmp_seq=3 ttl=64 time=2.94 ms
64 bytes from 192.168.0.168: icmp_seq=4 ttl=64 time=0.388 ms

--- 192.168.0.168 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3292ms
rtt min/avg/max/mdev = 0.304/0.997/2.944/1.124 ms

]$ ping 192.168.0.18

PING 192.168.0.18 (192.168.0.18) 56(84) bytes of data.
From 192.168.0.181 icmp_seq=2 Destination Host Unreachable
From 192.168.0.181 icmp_seq=3 Destination Host Unreachable
From 192.168.0.181 icmp_seq=4 Destination Host Unreachable

--- 192.168.0.18 ping statistics ---
4 packets transmitted, 0 received, +3 errors, 100% packet loss, time 3292ms
pipe 3

]$ perl test.pl 192.168.0.18

192.168.0.18 is down 

I had no clue even I have increase ping timeout but the results same

Upvotes: 2

Views: 4481

Answers (2)

CyberDem0n
CyberDem0n

Reputation: 15056

In order to send icmp packets you must have rights to create raw sockets, i.e. have root rights.
I suppose you running ping.pl as ordinary user, but you need to be root

ls -al `which ping`
-rws--x--x 1 root root 39640 Dec 17  2011 /bin/ping
   ^
   |
   suid bit

ping program has a suid bit, which allows to run ping program with root rights.

By default Net::Ping tries to connect to echo port (7/tcp), if it gets ECONNREFUSED - this means that host is up but refuses connection (nothing is listening on that port). if connect breaks on timeout, this means that host is down.

But! I can block all connection to 7/tcp by firewall:

iptables -I INPUT -p tcp --dport 7 -j DROP

and... voila, i get down instead of alive

So, you should check firewall on your failure pinged host

Upvotes: 1

tauli
tauli

Reputation: 1420

The only problem i can think of is, that the ping command uses the ICMP protocol as default, while Net::Ping uses TCP. You can switch Net::Ping to ICMP by creating your object like this:

my $p = Net::Ping->new( 'icmp' );

Take note, that making ICMP pings requires root privilege on Unix.

Upvotes: 6

Related Questions