#
#       Perl NMAP stub v2 (skeleton)         -- tested with nmap 2.07
#       
#       by .rain.forest.puppy.     rfpuppy@iname.com
#
#       v2: changed @udp/@tcp to %udp/%tcp; added smurf and state support
#

while(<>){ %udp=%tcp={}, $udp=$tcp=0, $Index=$OS=$IP=$Name=$Host="";
(($$3{$1}=$2) && $$3++) while(m#([0-9]+)/([a-z]+)/(udp|tcp)/\w*/\w*///[,]*#g);
$$1=$2 while(m#([^ \t\n:]+):\W*([^\t\n]+)#g);
(($Smurf=$1) && next) if (m#Status: Smurf\W*\(\W*([0-9]+)#);
($Host=~m#([\w\.]+)\W*\(([^ \t]*)\)#) && (($IP=$1) && ($Name=$2)) ;
#} complete code to parse nmap machine logfile -- gained one line since v1

# Usage:        cat nmap_machine_output_file | perl this_program.pl
#       OR      perl this_program.pl < nmap_machine_output_file
#       OR      perl this_program.pl nmap_machine_output_file
#
# provides      %udp, %tcp (hashes; key=portnum, value=state)
#               $udp, $tcp (number of ports for each)
#               $IP      (ip address in string format)
#               $Name      (reverse DNS hostname, if avail)
#               $Index     (Sequence Index, if avail)
#               $OS        (OS name, if avail)
#               $Smurf     (Number of smurf responses, if avail)
##########################################################
#  Put your code here
##########################################################

# Example/demo code to show usage

# normal info variables
print "IP: $IP" ;
print " Hostname: $Name" if $Name;
print " (Seq: $Index OS: $OS)" if ($OS ne "");

# how to step through all ports -- unordered
print "\nTotal TCP ports: $tcp\n";
foreach $pnum (keys %tcp){
printf "%20s", "$pnum=$tcp{$pnum}";}

# how to step through all ports -- sorted
print "\nTotal UDP ports: $udp\n";
@tmp=sort {$a <=> $b} (keys %udp);
foreach $pnum (@tmp){
printf "%20s", "$pnum=$udp{$pnum}";}

# how to query if a particular port was found
print "\n** Possibly running webserver" if (exists $tcp{80});

# how to check the specific state of a port
print "\n** Telnet port open" if ($tcp{23} eq "open");

print "\n\n";

### End of your code #####################################
}

