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!
In reply to Ping not working by ja6025
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |