subnet scanner utility in perl using Net::Ping

I often times have to assign an ipv4 address to a device at work and need to know what addresses are available on a particular subnet.  On other occasions, I just need to know what hosts are up / down on a subnet.  Normally, I would use nmap on my personal laptop, but only authorized devices are allowed on my company network, and I certainly want to stay out of trouble. Plus, I have to use windows 10 at work, and I do not have admin privelages.  I wrote a simple perl script that scans a user-defined range of ip addresses using the Net::Ping module from cpan.

This is the first of a suite of network utilities that I am writing to help troubleshoot network issues in cases where I do not have nmap available.   Here is a scan of a small portion of my home network:

Perl based subnet scanner using Net::Ping
Perl based subnet scanner using Net::Ping

Here is the source code so far:

 

#!/usr/bin/perl -w
use Net::Ping;
use Time::HiRes qw(gettimeofday);
use Term::ANSIColor;
use 5.010;
 
print "ping tester\n--------------------------\n";
#print "enter ip: ";

#get address
#chomp(my $host = );
#print "host: $host\n";

#create ping object
my $p = Net::Ping->new('icmp');
#hi res time
$p->hires();

#a single ip address
my $host;

#up / down devices
my @online_devices; 
my @offline_devices; 

#get address range from user
print "Enter IP address range: ";
chomp(my $ip_range = );
#print "range: $ip_range\n";
$ip_range =~ /(\d+\.\d+\.\d+)\.(\d+)-(\d+)/;
my $network = $1;
my $start_ip = $2;
my $end_ip = $3;
#print "start IP: $start_ip\nend IP: $end_ip\n";

#get t0 for benchmark
my $t0 = gettimeofday();

for(my $i=$start_ip;$i<=$end_ip;$i++)	{
	$host = "$network.$i";
	print "checking $host....\n";
	
	#list context, returns duration
	my ($ret, $dur, $ip) = $p->ping($host, 0.25);
	
	#format time
	$dur = sprintf("%.6f", $dur); 	
	
	#results
	if($ret){
		print colored  ("$host is up  latency: $dur seconds", 'white on_green');
		print "\n";
		push @online_devices, $host;
	}#end if
	else{
		print colored ("$host down", 'white on_red');
		print "\n";
		push @offline_devices, $host;
	}#end else		
	print "-----------------------------\n";
}#end for

#kill ping object
$p->close();

#benchmarking results
my $t1 = gettimeofday();
my $elapsed = $t1 - $t0; 
$elapsed = sprintf("%6f", $elapsed);
print "\ntime elapsed: $elapsed....\n";

#report file
open RP, ">", "report.txt" or die $!;

#results
my $up_sz = @online_devices;
my $down_sz = @offline_devices;
print "$up_sz devices online\n$down_sz devices offline\n";

print "\nOnline devices....\n------------------------------------------------------\n";
print RP "Online devices....\n------------------------------------------------------\n";
foreach $a (@online_devices){
	say $a;
	print RP $a."\n"; 
}#end foreach

print "\n\nOffnline devices....\n------------------------------------------------------\n";
print RP "Offline devices....\n------------------------------------------------------\n";
foreach $a (@offline_devices){
	say $a;
	print RP $a."\n"; 
}#end foreach

close RP;

Leave a Reply

Your email address will not be published. Required fields are marked *