Pingsweep – Perl

This is a very basic pingsweep tool I wrote in Perl using the Net::Ping library. NMAP does it better, and faster.

CODE:

#!/usr/bin/perl
 
use Net::Ping;
use Getopt::Std;
use Getopt::Long;
 
print_intro();
 
if(!$ARGV[0]) {#make sure there are at least ONE command line argument
	print "\tWhat? you need (-h)elp?\n\n";
	exit;
}
 
getopts('s:e:t:hv');	#get the options that the user has passed
 
if($opt_h) { 		#see if the help option was passed...
	print_menu();
	exit;
} else {
 
	if(!$opt_s || !$opt_e) {
		print "\tWhat? you need (-h)elp?\n\n";
		end;
	}
 
	if(!$opt_t) {
		$timeout = 1;
	} else {
		$timeout = $opt_t;
	}
 
	doscan();
	exit;
}#end if
 
sub print_intro() {
	print "\nPing Sweep Utility -- http://www.phishbone.org\n\n";
	print "\tPlease see -h for usage instructions\n\n";
}
 
sub print_menu() {
	print "\tProgram Options:\n";
	print "\t\t-s : the start address\n";
	print "\t\t-e : the end address\n";
	print "\t\t-t : the ICMP timeout\n";
	print "\t\t-v : verbose output\n";
	print "\t\t-h : display this menu :p\n";
	print "\n";
	print "\tProgram Usage: ./pingsweep.pl -s 192.168.1.1 -e 192.168.1.254 -t 0.1\n\n";
}#end sub print_menu
 
sub doscan() {
	$p = Net::Ping->new();
 
	@startrange = split(/\./, $opt_s);
	@endrange = split(/\./, $opt_e);
 
	for($x=$startrange[0];$x<=$endrange[0];$x++) {
		for($y=$startrange[1];$y<=$endrange[1];$y++) {
			for($z=$startrange[2];$z<=$endrange[2];$z++) {
				for($i=$startrange[3];$i<=$endrange[3];$i++) {
					$host = $x . "." . $y . "." . $z . "." . $i;
					if($opt_v) {
						print "Pinging $host\n";
					}
					if($p->ping($host, $timeout)) {
						print "\t" .  $host . " is alive.\n";
					}#end if
				}#end for 4
			}#end for 3
		}#end for 2
	}#end for 1
 
	print "All hosts from $opt_s to $opt_e scanned, see above for details...\n\n";
}#end sub doscan