5 |
|
|
6 |
use XML::Simple; |
use XML::Simple; |
7 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
8 |
|
use CouchDB; |
9 |
|
|
10 |
use client; |
use client; |
11 |
|
|
14 |
|
|
15 |
my $path = $what; |
my $path = $what; |
16 |
$path =~ s{[^\w\d\.]+}{_}g; |
$path =~ s{[^\w\d\.]+}{_}g; |
17 |
|
$path = "/tmp/nmap.$path"; |
18 |
|
|
19 |
warn "# scan $what"; |
warn "# scan $what"; |
20 |
|
|
21 |
system("nmap -O -oX /tmp/$path $what") == 0 || return; |
unlink $path if -f $path; |
22 |
|
|
23 |
|
system("nmap -O -oX $path $what"); |
24 |
|
|
25 |
my $nmap = XMLin( |
my $nmap = XMLin( |
26 |
"/tmp/$path", |
"$path", |
27 |
KeyAttr => { address => 'addrtype' }, |
KeyAttr => { address => 'addrtype' }, |
28 |
ForceArray => [ 'host', 'osmatch', 'port' ], |
ForceArray => [ 'host', 'address', 'osmatch', 'port' ], |
29 |
); |
); |
30 |
|
|
31 |
|
CouchDB::audit( $what, $nmap ); |
32 |
|
|
33 |
my $count = 0; |
my $count = 0; |
34 |
|
|
35 |
foreach my $host ( @{ $nmap->{host} } ) { |
while ( my $host = shift @{ $nmap->{host} } ) { |
36 |
|
|
37 |
warn dump( $host ); |
warn '# host ',dump( $host ); |
38 |
|
|
39 |
my $ip = $host->{address}->{ipv4}->{addr} || die "ipv4"; |
my $ip = $host->{address}->{ipv4}->{addr} || die "ipv4"; |
|
my $mac = $host->{address}->{mac}->{addr} || die "mac"; |
|
40 |
|
|
41 |
print "$ip\t$mac\n"; |
if ( my $mac = $host->{address}->{mac}->{addr} ) { |
42 |
|
print "$ip\t$mac\n"; |
43 |
|
client::save_ip_mac( $ip, $mac ); |
44 |
|
} |
45 |
|
|
|
client::save_ip_mac( $ip, $mac ); |
|
46 |
if ( my $os = $host->{os}->{osmatch}->[0]->{name} ) { |
if ( my $os = $host->{os}->{osmatch}->[0]->{name} ) { |
47 |
client::conf( $ip, 'os' => $os ); |
client::conf( $ip, 'os' => $os ); |
48 |
} |
} |
49 |
|
|
50 |
my @ports = map { $_->{portid} } @{ $host->{ports}->{port} }; |
my @ports = map { [ $_->{portid}, $_->{protocol}, $_->{service}->{name} ] } @{ $host->{ports}->{port} }; |
51 |
client::conf( $ip, 'ports' => join("\n", @ports) ) if @ports; |
client::conf( $ip, 'ports' => join("\n", map { join("\t", @$_) } @ports) ) if @ports; |
52 |
|
|
53 |
$count++; |
$count++; |
54 |
} |
} |