http://www.perlmonks.org?node_id=681164

Do you know when a new server or network device is added to your network? Does someone else let you know a week later that a new device needs to be monitored? Are you putting a list of IP's together in a text file and writing a for loop to ping each of these servers? How about an IP iterator that walks from a start IP to end end IP.

#!/usr/bin/perl -w use strict; use Net::Ping; use IO::Socket; sub ip_fr_dotted { unpack 'N', pack 'C4', split /\./, $_[0] } sub ip_to_dotted { join '.', unpack 'C4', pack 'N', $_[0] } my $start = ip_fr_dotted($ARGV[0]); my $end = ip_fr_dotted($ARGV[1]); for (my $ip=$start; $ip<=$end; $ip++) { print("\n ", ip_to_dotted($ip)); &ping_ip($ip); } sub ping_ip{ my $p = Net::Ping->new("icmp",2); if( $p->ping($_[0]) ){ print " available "; &id_device($_[0]); } $p->close(); } sub id_device { my $host = shift || ip_to_dotted($_[0]); #windows if( &port_check('tcp',$host,'139') == 0 && &port_check('udp',$host,'13 +8') == 0 && &port_check('udp',$host,'137') == 0 ){ print "windows"; } } sub port_check{ my $handle = IO::Socket::INET->new (Proto=>$_[0], PeerAddr=>$_ +[1], PeerPort=>$_[2] ); if($handle){ return 0; }else{ return 1; } }