Skip to content

Instantly share code, notes, and snippets.

@schrodyn
Created June 2, 2016 15:27
Show Gist options
  • Save schrodyn/c64f30e7d032ed5400a4ab26eb830b15 to your computer and use it in GitHub Desktop.
Save schrodyn/c64f30e7d032ed5400a4ab26eb830b15 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
#
#
# Host: 10.10.9.8 () Ports: 21/open/tcp//ftp//Microsoft ftpd/, 25/open/tcp//smtp//Microsoft ESMTP 6.0.3790.4675/,
#
#
use strict;
use warnings;
use Data::Dumper;
my %hosts = ();
my $dir = ".";
opendir my $dh, $dir or die "Could not open '$dir' for reading: $!\n";
my @things = readdir $dh;
my @nmap_fields;
foreach my $thing (@things) {
next if $thing !~ /.gnmap$/;
#print "File: $thing\n";
if (open(my $fh, '<:encoding(UTF-8)', $thing)) {
while (my $row = <$fh>) {
chomp $row;
next if $row =~ /^#/;
if ($row =~ /^Host:.*open\//) {
@nmap_fields = split("\t", $row);
}
else { next; }
my $host = $1 if $nmap_fields[0] =~ /(\d+\.\d+\.\d+\.\d+)/;
my $ports_line = $1 if $nmap_fields[1] =~ /Ports:\s+(.*)/;
my @services = split(/(?:\/$)|(?:\/,\s*)/, $ports_line);
foreach my $service (@services) {
my ($port, $state, $protocol, $owner, $service,
$rpc_info, $version) = split('/', $service);
push @{$hosts{$host}}, ($port, $service, $version) if ! (grep /$port/,
@{$hosts{$host}}) && ($state =~ /open/);
}
}
} else {
warn "Could not open file '$thing' $!";
}
}
foreach my $host (sort keys %hosts) {
print "$host\n";
for (my $i = 0; $i < @{$hosts{$host}}; $i+=3) {
printf " - %5d %s %s\n", @{$hosts{$host}}[$i],
@{$hosts{$host}}[$i+1], @{$hosts{$host}}[$i+2];
}
print "\n";
}
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment