You perform a test to see if you matched the line with PING, but then perform the print on every cycle. Is this script more like what you want?
#!/usr/bin/perl use strict; use warnings; my $count = 1; my $interval = 5; my $pkt_size = 128; my $timeout = 5; my $phost = 'www.google.com'; open CMD, "ping -c $count -i $interval -s $pkt_size -w $timeout $phost + |" or die "Can't open ping: $!"; while (<CMD>) { # look for the line like: PING www.google.com (10.160.83.59): 56 d +ata bytes next unless /PING\s+(\S+)\s+\(([\d.]+)\)/; print "host: $1\n"; print "IP: $2\n"; }
If you want to keep the values outside the loop: define the variable before the loop, then put the host/ip into the variables when you find them. Print at your leisure.
#!/usr/bin/perl use strict; use warnings; my $count = 1; my $interval = 5; my $pkt_size = 128; my $timeout = 5; my $phost = 'www.google.com'; open CMD, "ping -c $count -i $interval -s $pkt_size -w $timeout $phost + |" or die "Can't open ping: $!"; my $host; my $ip; while (<CMD>) { # look for the line like: PING www.google.com (10.160.83.59): 56 d +ata bytes next unless /PING\s+(\S+)\s+\(([\d.]+)\)/; $host = $1; $ip = $2; } print "host: $host has IP: $ip\n";
Cheers,
R.
In reply to Re: uninitialized values
by Random_Walk
in thread uninitialized values
by gaurav
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |