#!/usr/bin/perl -w use strict; use Net::Ping; use Getopt::Std; $|++; my %opts; getopts('?ht:s:',\%opts); if ($opts{'h'} or $opts{'?'}) { print "Usage:\n"; print "\t$0 [-t timeout] [-s subnet]\n"; exit; } $opts{'t'} ||= 5; unless ($opts{'s'}) { print "Enter subnet: "; chomp($opts{'s'} = ); } $opts{'s'} =~ s/^(\d+)\.(\d+)\.(\d+).*/$1.$2.$3/ or die "Bad subnet format: use xxx.xxx.xxx"; my (@up,@down); my @ips = map {"$opts{'s'}.$_"} 1..254; my $ping = Net::Ping->new("icmp",$opts{'t'}); $SIG{INT} = \&cleanup; for (@ips) { if ($ping->ping($_)) { print "Host $_ is UP\n"; push @up, $_; } else { print "Host $_ is DOWN\n"; push @down, $_; } } cleanup(); sub cleanup { print "Cleaning up and writing output files.."; open UP_FILE, ">ips_online" or die "Can't create online ip file: $!"; print UP_FILE map {"$_\n"} @up; close UP_FILE or die "Can't close online ip file: $!"; open DOWN_FILE, ">ips_offline" or die "Can't create offline ip file: $!"; print DOWN_FILE map {"$_\n"} @down; close DOWN_FILE or die "Can't close offline ip file: $!"; print "Done.\n"; exit; }