This solution reads in a line from a text file that is passed in as input parameter 1 to a Perl script. This script will then ping the machine using TCP pings to the remote hosts echo port and will return up/down and the ping response time. A second optional attribute may be passed in that will set the default timeout for the ping response.
Text file format:
127.0.0.0 localhost
192.168.100.1 abc
Now i want to add some more colums like serial no like
1 127.0.0.0 localhost
2 192.168.100.1 abc
but when scripts reads text file does not give required information.
#!/usr/bin/perl
# This script uses perl's Ping library.
# The first parameter to pass in is a text file to open that contains a list
# of ip serverIDs. A sample would look like:
# 1.2.3.4 LDAPServer
# 127.0.0.1 localhost
# The second argument is the timeout that the ping command should have. This
# value should be 1,2,3,4, or 5 seconds. If nothing is specified the default of
# 5 seconds is used.
# The resulting output will result in each line of the script bing printed with
# "up" or "down"
Expand|Select|Wrap|Line Numbers
- use strict;
- use Net::Ping;
- use Time::HiRes;
- my $filename = $ARGV[0];
- my $interval = $ARGV[1];
- my @ips = undef;
- my $line = undef;
- my $p = undef;
- my $ret = undef;
- my $duration = undef;
- my $ip = undef;
- open(FILE, "< $filename") or die "Can't open $filename : $!";
- @ips = <FILE>;
- close FILE;
- if (($interval eq undef) || ($interval le 0) || ($interval gt 5)) {
- $interval = 5;
- }
- foreach $line (@ips) {
- $p = Net::Ping->new("tcp",$interval);
- $p->hires();
- ($ret, $duration, $ip) = $p->ping ($line);
- if ($ret) {
- chomp($line);
- printf("$line up %.2f\n", 1000 * $duration)
- } else {
- chomp ($line);
- print "$line down 0.00\n";
- }
- $p->close();
- }