hi,
i have been asked to get the information of the systems in the network by executing ping in perl programming.

use Net::Ping;
$p = Net::Ping->new();
print "$host is alive.\n" if $p->ping($host);
$p->close();

sample of my code is shown above

where $host--> is the ip of the destination machine. if i try to give the loopback or ip of my machine then its working properly. but if i try to give ip of the some other machine conneted to network then ping will not work.
But ping using command prompt is working properly for any machine in the network.
Is there anybody to help me out...

Dani AI

Generated

Short diagnosis and what likely went wrong: Net::Ping does several different kinds of “ping” (TCP, UDP, ICMP, etc.), and the behaviour you get depends on which protocol you ask for. A plain Net::Ping->new() call does not necessarily perform the same ICMP echo that the system ping command uses; some Net::Ping modes try the echo service (port 7) or open TCP connections, which will fail against hosts that don’t run that service or are behind a firewall. was on the right track suggesting ICMP if you want the same behaviour as ping. (perldoc.perl.org)

ICMP note and practical alternatives: using the icmp protocol requires the ability to open raw sockets (usually root or the CAP_NET_RAW capability on Linux). If you cannot or should not run your script privileged, use Net::Ping::External (it calls your system ping and parses the output) or invoke the system ping program directly; both approaches avoid raw-socket privileges and usually match the system ping’s behaviour. Granting CAP_NET_RAW to an interpreter or using setuid root are possible but have security implications; file capabilities (setcap) are the safer, system-supported option when appropriate. (perldoc.perl.org)

Quick, actionable troubleshooting steps (apply in order):

  • Explicitly choose the protocol instead of relying on the default.

  • If you want to test reachability without ICMP, point a TCP ping at a known-open port (HTTP/SSH) rather than the echo port. Example:

    use Net::Ping;
    my $p = Net::Ping->new('tcp', 2);
    $p->port_number(22);   # test SSH instead of echo
    print "$host reachable\n" if $p->ping($host);
    $p->close;

    This avoids the echo-service issue and quickly shows whether plain TCP reachability is working. Also try Net::Ping->new('external') or Net::Ping::External::ping(...) if you want the exact system-ping behaviour. (perldoc.perl.org)

Return-value and test advice: check ping as a boolean (if ($p->ping($host))) rather than comparing to an empty string; in list context ping can return timing and IP information if you need it. Finally, verify local/remote firewall rules and whether the remote host runs an echo service—those are the usual reasons the command-line ping succeeds while a default Net::Ping call does not. (perldoc.perl.org)

Recommended Answers

All 2 Replies

Net::Ping defaults to a tcp connection to the echo port (7 according to my /etc/services file). Use this (from the Net::Ping man page) example to do a regular ICMP ping like the command line:

use Net::Ping;
my $p = Net::Ping->new('icmp');
print "$host is ";
print "NOT " unless $p->ping($host, 2);
print "reachable.\n";
sleep(1);
$p->close();

Kordaff

Try this... Its working for me

my $a;
my $p;
use Net::Ping;
$p = Net::Ping->new();
$a= $p->ping("$host");
$p->close();
if( $a eq '')
{
print "Host is not alive";
}
else
{
print "Host is alive";
}


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.