5 |
|
|
6 |
use XML::Simple; |
use XML::Simple; |
7 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
8 |
|
use store; |
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 |
|
store::audit( $what, $nmap ); |
32 |
|
|
33 |
my $count = 0; |
my $count = 0; |
34 |
|
|
35 |
foreach my $host ( @{ $nmap->{host} } ) { |
my $port_name = { |
36 |
|
16992 => 'amt', |
37 |
|
4445 => 'asterisk', |
38 |
|
7777 => 'pxelator', |
39 |
|
}; |
40 |
|
|
41 |
warn dump( $host ); |
while ( my $host = shift @{ $nmap->{host} } ) { |
42 |
|
|
43 |
|
warn '# host ',dump( $host ); |
44 |
|
|
45 |
my $ip = $host->{address}->{ipv4}->{addr} || die "ipv4"; |
my $ip = $host->{address}->{ipv4}->{addr} || die "ipv4"; |
|
my $mac = $host->{address}->{mac}->{addr} || die "mac"; |
|
46 |
|
|
47 |
print "$ip\t$mac\n"; |
if ( my $mac = $host->{address}->{mac}->{addr} ) { |
48 |
|
print "$ip\t$mac\n"; |
49 |
|
client::save_ip_mac( $ip, $mac ); |
50 |
|
} |
51 |
|
|
|
client::save_ip_mac( $ip, $mac ); |
|
52 |
if ( my $os = $host->{os}->{osmatch}->[0]->{name} ) { |
if ( my $os = $host->{os}->{osmatch}->[0]->{name} ) { |
53 |
client::conf( $ip, 'os' => $os ); |
client::conf( $ip, 'os' => $os ); |
54 |
} |
} |
55 |
|
|
56 |
my @ports = map { $_->{portid} } @{ $host->{ports}->{port} }; |
my @ports = map { [ $_->{portid}, $_->{protocol}, $port_name->{$_->{portid}} || $_->{service}->{name} ] } @{ $host->{ports}->{port} }; |
57 |
client::conf( $ip, 'ports' => join("\n", @ports) ) if @ports; |
client::conf( $ip, 'ports' => join("\n", map { join("\t", @$_) } @ports) ) if @ports; |
58 |
|
|
59 |
$count++; |
$count++; |
60 |
} |
} |