#!/usr/bin/perl -w

###################################
# Host location and port scanning convenience script
#   by Gabe Lein
#   Oct. 11
#
# This is a little script to combine some nmap commands and put the output in 
# a user specified file. This takes some time, because nmap's ping scanning
# sends out ARP requests for *every* possible host on the specified range, and
# THEN nmap runs port scanning on each host found.
#
# Use the form:
# $ ./findscan.pl IP/CIDR (e.g. ./findscan.pl 10.1.5.108/24)
#
# This is also not very subtle. Any Web Admin worth their overalls is going to 
# notice a few hundred ARP requests in rapid succession. Keep in mind that you 
# can specify the range (via CIDR notation). This can get you some data quicker
# and be *less* obvious, though end results will take longer.
###################################
use strict;

my $DEBUG = 1;

my $ipRange = shift(@ARGV);
my @hosts;

print "Name a file to save to: \n";
my $fileName = <STDIN>;

`nmap -sP $ipRange >> nmaptemp.txt`; #Run nmap ping scan mode and output to file
open (INPUT, "<nmaptemp.txt") or die "Can't open file!\n";
    while (<INPUT>){push @hosts, $_;} #Access file and pass to hosts array
`rm nmaptemp.txt`;
for (1..2) {shift(@hosts)}
pop(@hosts);


for (my $i = 0; $i < @hosts; $i++){
    $hosts[$i] =~ s/.*\(//;
    $hosts[$i] =~ s/\).*//;
    chomp($hosts[$i]);
    print "$hosts[$i] \n" if $DEBUG;
    `nmap -sF $hosts[$i] >> $fileName`;
}

