ja6025 has asked for the wisdom of the Perl Monks concerning the following question:
Hello all, I am fairly new to perl, having learned it 10 years ago by self training, but then didn't use it until recently. I have some to ping an IP address using a subroutine that works with an array, or with an IP from STDIN, but not from a straight variable. I'm confused! Any help would be appreciated! Thanks! Here are 3 examples of the code, 2 that work, and the one I really need to work that doesn't:
# this does not work use strict; use Net::Ping; use warnings; my $ip = qw(172.16.1.2); # my $works = &pingIp($ip); # if ( $works == 1 ) { print "does not ping\n"; } else { print "pings\n"; } sub pingIp { my $ip1 = shift; my $p = Net::Ping->new(); if ($p->ping($ip1)) { print "return is 0\n"; return 0; } else { print "return is 1\n"; return 1; } $p->close(); }
If I prompt for an IP rather than set the IP outright, the above works
print "Enter an IP: "; chomp (my $ip=<STDIN>);
Or, if I create an array of IPs and ping each one using the pingIp subroutine, it works
my @IParray = qw(172.16.1.2 172.16.1.3); foreach my $ip (@IParray) { my $works = &pingIp($ip); if ( $works == 1 ) { print "$ip does not ping\n"; } else { print "$ip pings\n"; } }
Thanks for any help!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Ping not working
by VinsWorldcom (Prior) on Mar 11, 2015 at 14:52 UTC | |
|
Re: Ping not working
by toolic (Bishop) on Mar 11, 2015 at 13:32 UTC | |
by ja6025 (Initiate) on Mar 11, 2015 at 14:24 UTC | |
by toolic (Bishop) on Mar 11, 2015 at 14:31 UTC | |
|
Re: Ping not working
by karlgoethebier (Abbot) on Mar 11, 2015 at 12:54 UTC | |
by Anonymous Monk on Mar 11, 2015 at 17:53 UTC | |
by karlgoethebier (Abbot) on Mar 11, 2015 at 18:24 UTC | |
by Anonymous Monk on Mar 11, 2015 at 18:46 UTC | |
|
Re: Ping not working
by Anonymous Monk on Mar 11, 2015 at 15:04 UTC |