--- lib/PXElator/nmap.pm 2009/08/27 17:44:03 311 +++ lib/PXElator/nmap.pm 2009/08/29 12:55:34 337 @@ -5,6 +5,7 @@ use XML::Simple; use Data::Dump qw/dump/; +use CouchDB; use client; @@ -13,35 +14,41 @@ 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++; }