chris_henderson has asked for the wisdom of the Perl Monks concerning the following question:

This is my first ever program and I need some peer review on the code and also have a few questions. I've read the Net::Ping documentation and Learning Perl 4th edition.

use Net::Ping;
@host_array = ("4.4.4.4", "127.0.0.1", "123.333.333.333");

$p = Net::Ping->new("icmp");
foreach $host (@host_array) {
print "$host is dead\n" unless $p->ping($host, 2);
print "$host is alive.\n" if $p->ping($host);
}
$p->close();

- seems like I need to run the program as root or else I get an error: "icmp ping requires root privilege at perl_ping.pl line 10" - is there any way to fix this?

- what is the "2" in print "$host is dead\n" unless $p->ping($host, 2);?

- If I want to have IP addresses like: 1.1.1.0/8 or 203.10.22.19 to 203.10.22.255 is there any shortcut way to put this in the @array?

Thanks for any help.
-
 

Replies are listed 'Best First'.
Re: Net::Ping issues
by almut (Canon) on Nov 19, 2008 at 19:33 UTC
    icmp ping requires root privilege

    I don't think there's a way to 'fix' this...  The usual command line utility ping has the same problem, which is why it's set-user-id-root:

    $ ls -l `which ping` -rwsr-xr-x 1 root root 37656 Apr 23 2006 /bin/ping ^ ^
Re: Net::Ping issues
by zentara (Cardinal) on Nov 19, 2008 at 19:48 UTC
Re: Net::Ping issues
by toolic (Bishop) on Nov 19, 2008 at 19:38 UTC
    Welcome to the Monastery.

    It would have been better to enclose your code snippet in "code" tags. Please read Writeup Formatting Tips. It's not too late to edit your post, deleting all "br" tags, and adding "code" tags around your code.

    - what is the "2" in print "$host is dead\n" unless $p->ping($host, 2);?
    According to the documentation for Net::Ping, the "2" is a timeout value:
    $p->ping($host [, $timeout]);
    Ping the remote host and wait for a response. $host can be either the hostname or the IP number of the remote host. The optional timeout must be greater than 0 seconds and defaults to whatever was specified when the ping object was created.
    is there any shortcut way to put this in the @array?
    I'm not sure what you mean by shortcut, but qw is handy for constructing arrays (note the absence of double quotes and commas):
    my @array = qw(4.4.4.4 127.0.0.1 123.333.333.333);

    Another general tip: add these to the top of your code:

    use warnings; use strict;

    I have no answer for your 1st question :(

Re: Net::Ping issues
by zwon (Abbot) on Nov 19, 2008 at 20:20 UTC
    I think it would be better to rewrite loop in the following way:
    foreach $host (@host_array) { if( $p->ping($host, 2) ) { print "$host is alive.\n"; } else { print "$host is dead\n"; } }
    Or you can face situation when some host dead and alive (if first ping fails and second successful) or neither dead nor alive (if first ping succeeds and second fails).
Re: Net::Ping issues
by mr_mischief (Monsignor) on Nov 19, 2008 at 22:23 UTC
Re: Net::Ping issues
by leighsharpe (Monk) on Nov 20, 2008 at 00:08 UTC
    If I want to have IP addresses like: 1.1.1.0/8 or 203.10.22.19 to 203.10.22.255 is there any shortcut way to put this in the @array?
    1.1.1.0/8 is not a valid network address, by the way. Perhaps you want 1.0.0.0/8?
    Here's a way to produce a list of all IP addresses in a network, given it's subnet address and mask (in the form a.b.c.d/e)
    sub get_ips { my $subnet_specification=shift; # Get t +he subnet specification. my ($network, $mask)=split(/\//, $subnet_specification); + # Break it into a subnet and mask. my @subnet_array=split/\./,$network; # Spli +t the network into individual octets. my $subnet_value=$subnet_array[0]*0x1000000+$subnet_array[1]*0x100 +00+$subnet_array[2]*0x100+$subnet_array[3]; # $subnet_value now contains a 32-bit value corresponding to the n +etwork address given. # Now calculate the top end of the subnet. my $top_address=$subnet_value+(2**(32-$mask)-1); # We now have the first and last addresses required. Produce a lis +t of all addresses in between, in dotted-decimal format. my @return_value; # Holds the array + of addresses we are returning for (my $address=$subnet_value; $address<=$top_address; $address++ +) { my @octets=(int($address/0x1000000)%0x100, int($address/0x1000 +0)%0x100, int($address/0x100)%0x100, $address%0x100); push(@return_value, join(".", @octets)); # +Assemble it into a dotted-decimal form and add it to the returned arr +ay. } return @return_value; }
    Just call get_ips("192.168.0.0/24");
Re: Net::Ping issues
by Anonymous Monk on Jul 08, 2019 at 15:56 UTC
    Test with sudo perl archiveName.pl, it worked for me.