Reputation: 3
I just write this script however the script only print one result not the results from all devices. I belive my error is on print section but I couldn't figure out.
Note :- the host file has 30 devices list but the script print the result of the last device only.
#!/usr/bin/perl
$host_file = "/usr/local/bin/test/host2";
open (PACKETLOSS,"$host_file") or die "Cannot Open Extracted host file";
# Put Extracted data into an array
@extracted_array=<PACKETLOSS>;
chomp(@extracted_array);
foreach(@extracted_array) {
@words = split;
$host = $words[0];
}
$extracted_array[$ping_idx] = `/usr/sbin/ping -s -t 10 $host 56 2 2>&1`;
$ping_idx++;
($packet_loss) = ($ping =~ m/packets received, (\d+)% packet loss/);
($round_trip) = ($ping =~ m/round-trip.*\(ms\).*min\/avg\/max\/stddev = \d+\.\d+\/(\d+\.\d+)\/.*/);
print " $host $round_trip ms Average Latency and $packet_loss Packet loss\n";
Upvotes: 0
Views: 156
Reputation: 77934
Cause you are closing foreach and then performing the operation. IT should be
foreach(@extracted_array)
{
@words = split;
$host = $words[0];
$extracted_array[$ping_idx] = `/usr/sbin/ping -s -t 10 $host 56 2 2>&1`;
$ping_idx++;
($packet_loss) = ($ping =~ m/packets received, (\d+)% packet loss/);
($round_trip) = ($ping =~ m/round-trip.*\(ms\).*min\/avg\/max\/stddev = \d+\.\d+\/(\d+\.\d+)\/.*/);
print " $host $round_trip ms Average Latency and $packet_loss Packet loss\n";
}
Upvotes: 0
Reputation: 17461
Make your foreach loop close at the very bottom of the code.
foreach(@extracted_array) {
@words = split;
$host = $words[0];
$extracted_array[$ping_idx] = `/usr/sbin/ping -s -t 10 $host 56 2 2>&1`;
$ping_idx++;
($packet_loss) = ($ping =~ m/packets received, (\d+)% packet loss/);
($round_trip) = ($ping =~ m/round-trip.*\(ms\).*min\/avg\/max\/stddev = \d+\.\d+\/(\d+\.\d+)\/.*/);
print " $host $round_trip ms Average Latency and $packet_loss Packet loss\n";
}
Upvotes: 1