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