nicer scan result path /tmp/nmap.what, store scan result in CouchDB
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 29 Aug 2009 12:55:34 +0000 (12:55 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 29 Aug 2009 12:55:34 +0000 (12:55 +0000)
lib/PXElator/nmap.pm

index 7caac16..0ad47a3 100644 (file)
@@ -5,6 +5,7 @@ use strict;
 
 use XML::Simple;
 use Data::Dump qw/dump/;
+use CouchDB;
 
 use client;
 
@@ -13,35 +14,41 @@ sub scan {
 
        my $path = $what;
        $path =~ s{[^\w\d\.]+}{_}g;
+       $path = "/tmp/nmap.$path";
 
        warn "# scan $what";
 
-       system("nmap -O -oX /tmp/$path $what") == 0 || return;
+       unlink $path if -f $path;
+
+       system("nmap -O -oX $path $what");
 
        my $nmap = XMLin(
-               "/tmp/$path",
+               "$path",
                KeyAttr => { address => 'addrtype' },
-               ForceArray => [ 'host', 'osmatch', 'port' ],
+               ForceArray => [ 'host', 'address', 'osmatch', 'port' ],
        );
 
+       CouchDB::audit( $what, $nmap );
+
        my $count = 0;
 
-       foreach my $host ( @{ $nmap->{host} } ) {
+       while ( my $host = shift @{ $nmap->{host} } ) {
 
-               warn dump( $host );
+               warn '# host ',dump( $host );
 
                my $ip  = $host->{address}->{ipv4}->{addr} || die "ipv4";
-               my $mac = $host->{address}->{mac}->{addr}  || die "mac";
 
-               print "$ip\t$mac\n";
+               if ( my $mac = $host->{address}->{mac}->{addr} ) {
+                       print "$ip\t$mac\n";
+                       client::save_ip_mac( $ip, $mac );
+               }
 
-               client::save_ip_mac( $ip, $mac );
                if ( my $os = $host->{os}->{osmatch}->[0]->{name} ) {
                        client::conf( $ip, 'os' => $os );
                }
 
-               my @ports = map { $_->{portid} } @{ $host->{ports}->{port} };
-               client::conf( $ip, 'ports' => join("\n", @ports) ) if @ports;
+               my @ports = map { [ $_->{portid}, $_->{protocol}, $_->{service}->{name} ] } @{ $host->{ports}->{port} };
+               client::conf( $ip, 'ports' => join("\n", map { join("\t", @$_) } @ports) ) if @ports;
 
                $count++;
        }