source: ocsinventory-agent/trunk/fuentes/lib/Ocsinventory/Agent/Backend/IpDiscover/Nmap.pm @ 468

Last change on this file since 468 was 468, checked in by mabarracus, 4 years ago

Copyt trusty code

File size: 1.7 KB
Line 
1package Ocsinventory::Agent::Backend::IpDiscover::Nmap;
2
3use vars qw($runMeIfTheseChecksFailed);
4$runMeIfTheseChecksFailed = ["Ocsinventory::Agent::Backend::IpDiscover::IpDiscover"];
5use strict;
6use warnings;
7
8sub check {
9  my $params = shift;
10
11  return unless can_run("nmap");
12  return unless can_load("Nmap::Parser");
13
14  # Do we have nmap 3.90 (or >)
15  foreach (`nmap -v 2>&1`) {
16    if (/^Starting Nmap (\d+)\.(\d+)/) {
17      my $release = $1;
18      my $minor = $2;
19
20      if ($release > 3 || ($release > 3 && $minor >= 90)) {
21        return 1;
22      }
23    }
24  }
25
26  0;
27}
28
29
30sub run {
31  my $params = shift;
32
33  my $common = $params->{common};
34  my $prologresp = $params->{prologresp};
35  my $logger = $params->{logger};
36
37  # Let's find network interfaces and call ipdiscover on it
38  my $options = $prologresp->getOptionsInfoByName("IPDISCOVER");
39
40  my $network;
41  if ($options->[0] && exists($options->[0]->{content})) {
42    $network = $options->[0]->{content};
43  } else {
44    return;
45  }
46
47  unless ($network =~ /^\d+\.\d+\.\d+\.\d+$/) {
48    return;
49  }
50
51  #Let's go scanning the network and parsing the results
52  $logger->debug("Scanning the $network network");
53  my $nmaparser = new Nmap::Parser;
54  $nmaparser->parsescan("nmap","-sP","-PR","$network/24");
55
56  for my $host ($nmaparser->all_hosts("up")){
57    my $ip = $host->addr;
58    my $mac = $host->mac_addr;
59    my $hostname = $host->hostname;
60
61    if ($hostname eq 0) {
62      $hostname = undef;     #it's better to send nothing instead of a '0'
63    }
64
65    if ($mac) {
66      $logger->debug("Host $ip found using Nmap. Adding informations in XML");
67
68      #Feeding the Inventory XML
69      $common->addIpDiscoverEntry({
70        IPADDRESS => $ip,
71        MACADDR => lc($mac),
72        NAME => $hostname,
73     });
74    }
75  }
76}
77
78
791;
Note: See TracBrowser for help on using the repository browser.