I had to write a program that determined the packet loss of our switches and displayed the results only if the packet loss was more than 0%. However, what was needed was a GUI (which I later wrote in C#) but at this time I had created a shell script. Below is the code:
1) Add the IPs to $host[x] that you want to ping or remove the for loop and use the argument $host variable to ping.
2) Uncomment these lines to see all ping results. Those that failed will show in Yellow:
Code:# else { # print "IP: $h loss:$loss\n"; # }
Complete Script:
Code:#!/usr/bin/perl # For Testing use an argument #$host = $ARGV[0]; # Define our Hosts $host[0] = "msn.com"; $host[1] = "google.com"; # Loop through our hosts and ping foreach $h(@host) { $ping = `/bin/ping -c 2 -n -q $h`; my ($loss) = ($ping =~ /(\d+)%/g); my ($one,$rtt) = ($ping =~ /(\d+\.\d+)\//g); if ($rtt eq "") { $rtt = 0; $loss = 100; } # Display ping loss if ($loss > 0) { print "\033[33;1mIP: $h Ping-Loss: $loss%\033[0m\n"; } # else { # print "IP: $h loss:$loss\n"; # } }


LinkBack URL
About LinkBacks









Reply With Quote


Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum