[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3-ocs/sources/www/ -> ipdiscover-util.pl (source)

   1  #!/usr/bin/perl 
   2  ###############################################################################
   3  ##OCSINVENTORY-NG
   4  ##Copyleft Pascal DANEK 2005
   5  ##Web : http://ocsinventory.sourceforge.net
   6  ##
   7  ##This code is open source and may be copied and modified as long as the source
   8  ##code is always made freely available.
   9  ##Please refer to the General Public Licence http://www.gnu.org/ or Licence.txt
  10  ################################################################################
  11  #
  12  #
  13  use DBI;
  14  use XML::Simple;
  15  use Net::IP qw(:PROC);
  16  use strict;
  17  use Fcntl qw/:flock/;
  18  #Command line options : 
  19  #-analyse : nmap analyse and files for grep, human readable and xml generation
  20  #-net : Treat machine for the specified subnet
  21  #
  22  #
  23  ################
  24  #OPTIONS READING
  25  ################
  26  #
  27  my $option;
  28  #Specify a subnet
  29  my $net;
  30  #Subnet name
  31  my $filter;
  32  #Analyse and class the computers
  33  my $analyse;
  34  #Net for a given ip
  35  my $iptarget;
  36  my $masktarget;
  37  #If auto flag, running for all the subnet.csv subnet and generate files
  38  my $auto;
  39  #Search for  problems with computer election
  40  my $ipd;
  41  my $list;
  42  my $xml;
  43  my $cache;
  44  #Default values for database connection
  45  #
  46  # my $dbhost = 'localhost';
  47  # my $dbuser = 'ocs';
  48  # my $dbpwd = 'ocs';
  49  my $dbuser=`cat /var/www/se3/includes/dbconfig.inc.php | grep COMPTE_BASE | cut -d= -f2 | cut -d\\" -f2`;
  50  my $dbpwd=`cat /var/www/se3/includes/dbconfig.inc.php | grep PSWD_BASE | cut -d= -f2 | cut -d\\" -f2`;
  51  my $dbhost=`cat /var/www/se3/includes/dbconfig.inc.php | grep SERVEUR_SQL | cut -d= -f2 | cut -d\\" -f2`;
  52  my $db = 'ocsweb';
  53  my $dbp = '3306';
  54  #
  55  chomp($dbuser);
  56  chomp($dbpwd);
  57  chomp($dbhost);
  58  
  59  my %xml;
  60  my $ipdiscover;
  61  
  62  #Cleanup the directory
  63  &_cleanup();
  64  
  65  for $option (@ARGV){
  66    if($option=~/-a$/){
  67      $analyse = 1;
  68    }elsif($option=~/-auto$/){
  69      $auto = 1;
  70    }elsif($option=~/-cache$/){
  71      $cache = 1;
  72      $analyse = 1;
  73      $xml = 1;
  74    }elsif($option=~/-ipdiscover=(\d+)$/){
  75      $ipdiscover = 1;
  76      $ipd = $1;
  77    }elsif($option=~/-xml$/){
  78      $xml = 1;
  79    }elsif($option=~/-h=(\S+)/){
  80      $dbhost = $1;
  81    }elsif($option=~/-d=(\S+)/){
  82      $db = $1;
  83    }elsif($option=~/-u=(\S+)/){
  84      $dbuser = $1;
  85    }elsif($option=~/-p=(\S+)/){
  86      $dbpwd = $1;
  87    }elsif($option=~/-P=(\S+)/){
  88      $dbp = $1;
  89    }elsif($option=~/-list$/){
  90        $list = 1;
  91    }elsif($option=~/-net=(\S+)/){
  92      die "Invalid subnet. Abort...\n" unless $1=~/^(\d{1,3}(?:\.\d{1,3}){3})$/;
  93      $net = 1;
  94      $filter = $1;
  95    }elsif($option=~/-ip=(\S+)/){
  96      die "Invalid address => [IP/MASK]. Abort...\n" unless $1=~/^(\d{1,3}(?:\.\d{1,3}){3})\/(.+)$/;
  97      $iptarget = $1;
  98      $masktarget = $2;
  99    }else{
 100      print <<EOF;
 101  Usage :
 102  -ip=X.X.X.X/X.X.X.X (ex: 10.1.1.1/255.255.240.0 or 10.1.1.1/20)-> Looks for the ip in a subnet
 103  -net=X.X.X.X -> Specify a network
 104  -a -> Launch the analyze
 105  -ipdiscover=X -> Show all the subnet with up to XX ipdiscover
 106  -xml -> xml output
 107  -list=show all the networks present in the database with "connected"/"discovered" computers
 108  #DATABASE OPTION
 109  -p=xxxx password (default ocs)
 110  -P=xxxx port (default 3306)
 111  -d=xxxx database name (default ocsweb)
 112  -u=xxxx user (default ocs)
 113  -h=xxxx (default localhost)
 114  
 115  EOF
 116      die "Invalid options. Abort..\n";
 117    }
 118  }
 119  if($analyse and !$net){
 120    die "Wich subnet do you want to analyse ?\n";
 121  } 
 122  if($cache or $auto){
 123    unless(-d "ipd"){
 124        mkdir("ipd") 
 125          or die $!;
 126    }
 127  }
 128  #Date of the day
 129  my $date = localtime();
 130  
 131  
 132  #######################
 133  #Database connection...
 134  ########
 135  #
 136  my $request;
 137  my $row;
 138  my $dbh = DBI->connect("DBI:mysql:database=$db;host=$dbhost;port=$dbp", $dbuser, $dbpwd)
 139   or die $!;
 140  
 141  #############################
 142  #We get the subnet/name pairs
 143  ####
 144  #
 145  my @subnet;
 146  $request = $dbh->prepare("SELECT * FROM subnet");
 147  $request->execute;
 148  while($row = $request->fetchrow_hashref){
 149    push @subnet, [ $row->{'NETID'}, $row->{'NAME'}, $row->{'ID'}, $row->{'MASK'} ];
 150  }
 151  
 152  ###########
 153  #PROCESSING
 154  ###########
 155  #
 156  if($auto){
 157    print "\n\n########################\n";
 158    print "Starting scan of subnets\n";
 159    print "########################\n";
 160    my %subnet;
 161    for(@subnet){
 162       my $name = $_->[1];
 163       my $netn  = $_->[2];
 164       $name=~s/ /_/g;
 165       $name=~s/\//\\\//g;
 166       $subnet{$name} = $netn;
 167       print "Retrieving $name (".$subnet{$name}.")\n";
 168    }
 169    my $i;
 170    print "\n\n##################\n";
 171    print "PROCESSING SUBNETS \n";
 172    print "##################\n\n";
 173    for(keys(%subnet)){
 174      print "Processing $_ (".$subnet{$_}."). ".(keys(%subnet)-$i)." networks left.\n";
 175      open OUT, ">ipd/".$subnet{$_}.".ipd" or die $!;
 176      unless(flock(OUT, LOCK_EX|LOCK_NB)){
 177        if($xml){
 178          print "<ERROR><MESSAGE>345</MESSAGE></ERROR>";
 179      exit(0);
 180        }else{
 181          die "An other analyse is in progress\n";
 182        }
 183      }
 184      system("./ipdiscover-util.pl -net=".$subnet{$_}.($xml?' -xml':'')." -a > ipd/'".$subnet{$_}.".ipd'");
 185      $i++;
 186    }
 187    system ("rm -f ipdiscover-analyze.*");
 188    print "Done.\n";
 189    exit(0);
 190  }
 191  
 192  #Host subnet
 193  my $network;
 194  #Subnet mask in binary format
 195  my $binmask;
 196  
 197  
 198  
 199  if($ipdiscover){
 200    my @networks;
 201    #get the subnets
 202    my $result;
 203    die "Invalid value\n" if $ipd<0;
 204    $request = $dbh->prepare('select distinct(ipsubnet) from networks left outer join devices on tvalue=ipsubnet where tvalue is null');
 205    $request->execute;
 206    while($row = $request->fetchrow_hashref){
 207      push @networks, [ $row->{'ipsubnet'}, '0' ];
 208    }
 209    $request->finish;
 210    #If positive value, it includes other subnet
 211    if($ipd){
 212      $request = $dbh->prepare('select count(*) nb,tvalue,name from devices group by tvalue having nb<='.$ipd.' and name="ipdiscover"');
 213      $request->execute;
 214      while($row = $request->fetchrow_hashref){
 215        push @networks, [ $row->{'tvalue'}, $row->{'nb'} ];
 216      }
 217      $request->finish;
 218    }
 219    print <<EOF unless($xml);
 220  ############################
 221  #IPDISCOVER Ver 1.0b
 222  #IP DISCOVER IP/SUBNET REPORTS
 223  #$date
 224  #Subnets with max $ipd 
 225  #  ipdiscover computers
 226  ############################
 227  
 228  
 229  EOF
 230  #
 231    my $output;
 232    my ($i,$j);
 233    for $network (@networks){
 234      my $ip = $network->[0];
 235      my $nbipd = $network->[1];
 236      next unless $ip =~ /^\d{1,3}(?:\.\d{1,3}){3}$/;
 237      my $req = $dbh->prepare('select h.deviceid, h.name, h.quality,h.fidelity,h.lastcome,h.lastdate,osname, n.ipmask, n.ipaddress from hardware h,networks n where n.deviceid=h.deviceid and n.ipsubnet='.$dbh->quote($ip).' order by lastdate'); 
 238      $req->execute;
 239      #Get the subnet label
 240      unless($xml){
 241        print "#######\n";
 242        my ($nname, $nuid) = &_getnetname($ip, '');
 243        print "SUBNET = ".$ip."-> $nbipd ipdiscover, ".($req->rows?$req->rows:0)." host(s) connected \n[ $nname ($nuid) ]\n";
 244        print "#\n\n";
 245        printf("     %-25s %-9s %-9s %-25s %-15s %-15s %s\n", "<Name>","<Quality>","<Fidelity>","<LastInventory>","<IP>","<Netmask>","<OS>");
 246        print "-----------------------------------------------------------------------------------------------------------------------\n";
 247        while($result = $req->fetchrow_hashref){
 248          my $r = $dbh->prepare('select * from devices where deviceid='.$dbh->quote($result->{'deviceid'}).' and tvalue='.$dbh->quote($ip).' and name="ipdiscover"');
 249      $r->execute;
 250      printf("#-> %-25s %-9s %-9s %-25s %-15s %15s %s %s\n",$result->{'name'},$result->{'quality'},$result->{'fidelity'},$result->{'lastdate'},$result->{'ipaddress'}, $result->{'ipmask'},$result->{'osname'} ,$r->rows?'*':'');
 251      $r->finish;
 252        }
 253        print "\n\n\n\n";
 254      }else{
 255        $xml{'SUBNET'}[$i]{'IP'} = [ $ip ];
 256        $xml{'SUBNET'}[$i]{'IPDISCOVER'} = [ $nbipd ];
 257        $xml{'SUBNET'}[$i]{'HOSTS'} = [ $req->rows?$req->rows:0 ];
 258        $j = 0;
 259        while($result = $req->fetchrow_hashref){
 260          $xml{'SUBNET'}[$i]{'HOST'}[$j]{'NAME'} = [ $result->{'name'} ];
 261      $xml{'SUBNET'}[$i]{'HOST'}[$j]{'QUALITY'} = [ $result->{'quality'} ];
 262      $xml{'SUBNET'}[$i]{'HOST'}[$j]{'FIDELITY'} = [ $result->{'fidelity'} ];
 263      $xml{'SUBNET'}[$i]{'HOST'}[$j]{'LASTDATE'} = [ $result->{'lastdate'} ];
 264      $xml{'SUBNET'}[$i]{'HOST'}[$j]{'OSNAME'} = [ $result->{'osname'} ];
 265      $xml{'SUBNET'}[$i]{'HOST'}[$j]{'IPMASK'} = [ $result->{'ipmask'} ];
 266      $j++;
 267        }
 268        $i++;
 269      }
 270    }
 271  if($xml){
 272    $output=XML::Simple::XMLout( \%xml, RootName => 'NET', SuppressEmpty => undef);
 273    print $output;
 274  }
 275  exit(0);
 276  }
 277  
 278  #############
 279  ##IP resolving
 280  ##############
 281  ##
 282  if($iptarget){
 283    my $netname;
 284    #If necessary, ascii conversion of a binary format
 285    $masktarget = _bintoascii($masktarget) if($masktarget=~/^\d\d$/);
 286    die "Invalid netmask. Abort.\n" unless $masktarget=~/^\d{1,3}(\.\d{1,3}){3}$/;
 287    $network = _network($iptarget, $masktarget);
 288    my $uid;
 289    ($netname, $uid)= &_getnetname($network, '-');
 290    my @nmb =  `nmblookup -A $iptarget`;
 291    #DNS name
 292    my $dnsname = &_getdns($iptarget);
 293    #Netbios name
 294    my $nmbname;
 295    my $inv;
 296    my $type;
 297    
 298    for(@nmb){
 299      $nmbname = $1,last if /\s+(\S+).*<00>/;
 300    }
 301    $request = $dbh->prepare('SELECT * FROM networks WHERE IPADDRESS='.$dbh->quote($iptarget));
 302    $request->execute;
 303    if($request->rows){
 304        $inv = 1;
 305      $type = 'Computer';
 306    }else{
 307        $request = $dbh->prepare('SELECT IP,TYPE FROM netmap, networks WHERE MAC=MACADDR AND DEVICEID like "NETWORK_DEVICE%" AND IP='.$dbh->quote($iptarget));
 308        $request->execute; 
 309      if(my $row = $request->fetchrow_hashref){
 310          $inv = 1, $type = $row->{'TYPE'};
 311      }
 312    }
 313    
 314    $request = $dbh->prepare('SELECT MAC FROM netmap WHERE IP='.$dbh->quote($iptarget));
 315    $request->execute;
 316    my $exist = 1 if $request->rows;
 317    
 318  unless($xml){
 319      print <<EOF;
 320  
 321  
 322  #########################
 323  #IPDISCOVER Ver 1.0b
 324  #IP DISCOVER REPORT
 325  #$date
 326  ##########################
 327  
 328  
 329  EOF
 330  
 331      print "\n--> ".($netname).". ($uid) ($network)\n";
 332      print <<EOF;
 333  
 334  Netbios name : $nmbname
 335  DNS name     : $dnsname
 336  EOF
 337  
 338      printf("Inventoried  : %s\n",$inv?'Yes':'No');
 339      printf("Discovered   : %s\n",$exist?'Yes':'No');
 340      print "Type   : $type\n" if $inv;
 341      print "\nDone.\n\n";
 342      exit(0);
 343    }else{
 344      my $output;
 345      $xml{'IP'} = [ $iptarget ];
 346      $xml{'NETNAME'} = [ $netname ]; 
 347      $xml{'NETNUM'} = [ $network ];
 348      $xml{'NETBIOS'} = [ $nmbname ]; 
 349      $xml{'DNS'} = [ $dnsname ]; 
 350      $xml{'INVENTORIED'} = [ $inv?'yes':'no' ]; 
 351      $xml{'DISCOVERED'} = [ $exist?'yes':'no' ]; 
 352      $xml{'TYPE'} = [ $type ] if $inv; 
 353      
 354      $output=XML::Simple::XMLout( \%xml, RootName => 'IP', SuppressEmpty => undef);
 355      print $output;
 356      exit(0);
 357    }
 358  }
 359  
 360  
 361  #
 362  #Searching non-inventoried mac addresses
 363  #
 364  my %network;
 365  my @hosts;
 366  my $null;
 367  #   
 368  
 369  $request = $dbh->prepare('SELECT IP,MASK,MAC,DATE FROM netmap LEFT JOIN networks ON MACADDR=MAC where MACADDR IS NULL');  
 370  $request->execute;
 371  #  
 372  #Determine the subnets
 373  #
 374  #
 375  my %network_ipd;
 376  #
 377  while($row = $request->fetchrow_hashref){
 378    my $ip;
 379    my $netmask;
 380    #
 381    if($row->{'MASK'}){
 382      $ip = $row->{'IP'};
 383      $netmask = $row->{'MASK'};
 384      $network = _network($ip, $netmask);
 385      $binmask = _binmask($netmask);
 386      if($net){
 387        next unless $network eq $filter;
 388      }
 389      #Hosts count per subnet
 390      if($list and !$analyse and !$net){
 391        $network_ipd{$network}++;
 392      }else{
 393        $network{$network}++;
 394        push @hosts, [ $row->{'MAC'}, $ip , $row->{'DATE'}];
 395      }
 396    }else{
 397      $null++;
 398    }
 399  }
 400  
 401  my $total = 0;
 402  #We want ALL subnets in the database, even those that are not discovered
 403  if($list and !$analyse and !$net){
 404    $request = $dbh->prepare('SELECT IPADDRESS,IPMASK FROM networks');
 405    $request->execute;
 406    while($row = $request->fetchrow_hashref){
 407        my $ip;
 408        my $netmask;
 409        #
 410        if($row->{'IPMASK'}=~/^\d{1,3}(\.\d{1,3}){3}$/ and $row->{'IPADDRESS'}=~/^\d{1,3}(\.\d{1,3}){3}$/){
 411          $ip = $row->{'IPADDRESS'};
 412          $netmask = $row->{'IPMASK'};
 413          $network = _network($ip, $netmask);
 414          $network{$network}++;
 415        }
 416        #Hosts count per subnet
 417    }
 418    #We show the part of non-inventoried computers
 419    my $netnum;
 420    for $netnum (keys(%network)){
 421      $total+=$network{$netnum};
 422      for(keys(%network_ipd)){
 423        $network{$netnum}.= "/".$network_ipd{$_} if($_ eq $netnum);
 424      }
 425    }
 426  }
 427  
 428  ########
 429  #RESULTS
 430  ########
 431  #
 432  print <<EOF unless $xml;
 433  
 434  
 435  #########################
 436  IPDISCOVER Ver 1.0b
 437  IP DISCOVER REPORT
 438  $date 
 439  #########################
 440  
 441  
 442  
 443  EOF
 444  unless($xml){
 445    printf("%-35s %-5s %-20s %-4s\n", "<Name>","<UID>","<Subnet>","<Nb>");
 446    print "-------------------------------------------------------------------\n";
 447  }
 448  #net UID
 449  my $dep;
 450  #net name
 451  my $lib;
 452  #
 453  my $line;
 454  my $netn;
 455  #
 456  my $i;
 457  my $output;
 458  for $netn (keys(%network)){
 459    ($lib, $dep) = &_getnetname($netn,'-');  
 460    if($xml and !$analyse){
 461      $xml{'NETWORK'}[$i]{'LABEL'} = [ $lib ];
 462      $xml{'NETWORK'}[$i]{'UID'} = [ $dep ]; 
 463      $xml{'NETWORK'}[$i]{'NETNAME'} = [ $netn ]; 
 464      $xml{'NETWORK'}[$i]{'NETNUMBER'} = [ $network{$netn} ];
 465      $i++;
 466    }elsif(!$xml){
 467      printf("%-35s %-5s %-20s %-4s\n",$lib,$dep,$netn,$network{$netn});
 468      $total += $network{$netn} unless $list;
 469    }
 470  }
 471  if($xml and !$net){
 472    $output=XML::Simple::XMLout( \%xml, RootName => 'IPDISCOVER', SuppressEmpty => undef);
 473    print $output;
 474  }
 475  
 476  if($net){
 477    #
 478    my($n, $i);
 479    #Host names
 480    my @names;
 481    #
 482    unless($xml){
 483      print "\n---------------------------------------------\n";
 484      print "Unknown host(s) on $filter \n";
 485      print "---------------------------------------------\n\n";
 486    }
 487    my $output;
 488    for $n (@hosts){
 489      #Trying a DNS resolution
 490      push @$n, &_getdns($$n[1]);
 491      unless($analyse){
 492        if($xml){
 493          $xml{'HOST'}[$i]{'IP'} = [ $$n[1] ];
 494          $xml{'HOST'}[$i]{'MAC'} = [ $$n[0] ]; 
 495      $xml{'HOST'}[$i]{'DATE'} = [ $$n[2] ]; 
 496          $xml{'HOST'}[$i]{'NAME'} = [ $$n[3] ]; 
 497          $i++;
 498        }else{
 499          printf("=> %-20s %-20s %-20s %s\n",$$n[1],$$n[0],$$n[2],$$n[3]);
 500        }
 501      }
 502     }
 503      
 504    if(!$analyse and $xml){
 505      $output=XML::Simple::XMLout( \%xml, RootName => 'NET', SuppressEmpty => undef);
 506      print $output;
 507    }
 508  }
 509  
 510  ########
 511  #ANALYZE
 512  ########
 513  #
 514  #
 515  #
 516  #windows computers
 517  my @PCW;
 518  #Linux computers
 519  my @PCL;
 520  #net peripherals
 521  my @PR;
 522  #Phantom computers
 523  my @PH; 
 524  #At least one port filtered with one port open or closed
 525  my @PF;
 526  if($analyse){
 527    #directory creation for analyses file
 528    if($cache){
 529      open CACHE, ">ipd/$filter.ipd" or die $!;
 530      unless(flock(CACHE, LOCK_EX|LOCK_NB)){
 531        if($xml){
 532          print "<ERROR><MESSAGE>345</MESSAGE></ERROR>";
 533      exit(0);
 534        }else{
 535          die "An other analyse is in progress\n";
 536        }
 537      }
 538    }
 539    
 540    unless(@hosts){
 541      print "No unknown hosts of this network. Stop.\n\n" unless $xml;
 542      exit(0);
 543    }
 544    #If it's a global analyze, we don't display the results but just generate the files
 545    #Using nmap :
 546    # -> Connection on ports 135(msrpc), 80(web), 22(ssh), 23(telnet)
 547    # -> files ipdiscover-analyze.* generated (3 formats : .nmap(human), .gnmap(pour grep), .xml(xml)
 548    # -> No 'host up' detection
 549    #
 550    my @ips;
 551    push @ips, $$_[1] for(@hosts);
 552    #Check that there is no analyses of this network pending
 553    open NMAP, "+>$filter.gnmap";
 554    unless(flock(NMAP, LOCK_EX|LOCK_NB)){
 555        if($xml){
 556          print "<ERROR><MESSAGE>345</MESSAGE></ERROR>";
 557      exit(0);
 558        }else{
 559          die "An other analyse is in progress\n";
 560        }
 561      }
 562    #Analyse
 563    system("nmap -R -v @ips -p 135,80,22,23 -oG $filter.gnmap -P0 > /dev/null");
 564    #
 565    my @gnmap;
 566    if($net){
 567      @gnmap = <NMAP>;
 568      close NMAP;
 569      unlink "$filter.gnmap";
 570      #
 571      ###########
 572      #
 573      my $ref;
 574      my ($h, $w, $j, $r, $f, $l);
 575      REF:
 576      for $ref (@hosts){
 577        $h = $$ref[1];
 578           for(@gnmap){
 579         if(/Host: $h\b/){
 580           if(/135\/o/){
 581                 $PCW[$w] = $ref;
 582             $w++;
 583                 next REF;
 584               }elsif( (/22\/c.+23\/c.+80\/o.+135\/c/) or (/22\/c.+23\/o.+80\/c.+135\/c/) or (/22\/c.+23\/o.+80\/o.+135\/c/) or (/22\/o.+23\/o/) ){
 585                 $PR[$r] = $ref;
 586             $r++;
 587                   next REF;
 588               }elsif(/22\/f.+23\/f.+80\/f.+135\/f/){
 589                 $PH[$j] = $ref;
 590             $j++;
 591                 next REF;
 592             }elsif( (/\d\d\/f/) and ( (/\d\d\/o/) or (/\d\d\/c/) ) ){
 593               $PF[$f] = $ref;
 594             $f++;
 595               next REF;
 596               }else{
 597                 $PCL[$l] = $ref;
 598             $l++;
 599                 next REF;
 600               }
 601         }
 602           }
 603       }
 604     #Display results
 605     print "<NETANALYSE>\n" if $xml;
 606     print CACHE "<NETANALYSE>\n" if $xml;
 607     &_print(\@PCW, 'WINDOWS COMPUTERS');
 608     &_print(\@PCL, 'LINUX COMPUTERS');
 609     &_print(\@PR, 'NETWORK DEVICES');
 610     &_print(\@PF, 'FILTERED HOSTS');
 611     &_print(\@PH, 'PHANTOM HOSTS');
 612     print "</NETANALYSE>\n" if $xml;   
 613     print CACHE "</NETANALYSE>\n" if $xml;
 614    }
 615  }
 616  
 617  ##################
 618  #DISLAY SUBROUTINE
 619  ##################
 620  #
 621  sub _print{
 622     my $ref = shift;
 623     my $lib = shift;
 624     if(@$ref){
 625       my $nb;
 626       unless($xml){
 627         print "#" for(0..(length($lib)+3));
 628         print "\n";
 629         print "# ".$lib." #";
 630         print "\n";
 631         print "#" for(0..(length($lib)+3));
 632         print "\n";
 633         print "#----------------------------------------------------------------------------\n";
 634         printf("#%-20s %-20s %-25s %s\n", "IP address",  "MAC address", "DNS/Netbios", "Date");
 635         print "#----------------------------------------------------------------------------\n#\n";
 636       }
 637            
 638       $nb = 0;
 639       my $output;
 640       %xml = undef;
 641       for(@$ref){
 642      $$_[3] = &_smbname($$_[1]) if $$_[3] eq "-";
 643      if($xml){
 644        $xml{'IP'} = [ $$_[1] ];
 645            $xml{'MAC'} = [ $$_[0] ]; 
 646            $xml{'NAME'} = [ $$_[3] ];
 647        $xml{'DATE'} = [ $$_[2] ];
 648        $xml{'TYPE'} = ['WINDOWS'] if $lib =~/windows/i;
 649        $xml{'TYPE'} = ['LINUX'] if $lib =~/linux/i;
 650        $xml{'TYPE'} = ['FILTERED'] if $lib =~/filtered/i;
 651        $xml{'TYPE'} = ['NETWORK'] if $lib =~/network/i;
 652        $xml{'TYPE'} = ['PHANTOM'] if $lib =~/phantom/i;
 653        $output=XML::Simple::XMLout( \%xml, RootName => 'HOST', SuppressEmpty => undef);
 654            print $output;
 655        print CACHE $output if $cache;
 656      }else{
 657        printf("#-> %-15s %-20s %-20s %s\n",$$_[1],$$_[0],$$_[3],$$_[2]);
 658        $nb++;
 659      }
 660       }
 661     unless($xml){
 662       print "#\n#==========> $nb host(s)\n#\n\n\n";
 663     }
 664    }
 665  }
 666  #
 667  #########################################
 668  #ROUTINE DE RECUPERATION DES NOMS NETBIOS
 669  #########################################
 670  #
 671  sub _smbname{
 672    my $flag = 0;
 673    my $name;
 674    my $ip = shift;
 675    #If no dns name, we try  a netbios resolution
 676    my @smb = `nmblookup -A $ip`;
 677    #On cherche un enregistrment <00> pour l'ip passee en argument
 678    for(@smb){
 679      $name = "-";
 680      /Looking\D+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/ unless $flag;
 681      if(/<00>/){
 682        /^\s+(\S+)/;
 683        $name = $1;
 684        return $name;
 685      }
 686    }
 687    return "-";
 688  }
 689  
 690  ##############
 691  #CLEAN *.gnmap
 692  ##############
 693  #
 694  sub _cleanup{
 695    my($name, @files);
 696    opendir DIR, ".";
 697    while($name = readdir DIR){
 698      push @files, $name if $name=~/\.gnmap$/i;
 699    }
 700    closedir DIR;
 701    for(@files){
 702      open FILE, $_ or next;
 703      unlink $_ if(flock(FILE, LOCK_EX|LOCK_NB));
 704    }
 705  }
 706  
 707  print "|\n|\n---------------------------------------------\n|" unless $xml;
 708  if($list){
 709    print "\n|--> TOTAL = $total valid (ips/netmask) in the database\n" unless $xml;
 710    exit(0);
 711  }
 712  
 713  unless($xml){
 714    print "\n|--> TOTAL = $total unknown hosts\n";
 715    print "\n|--> WARNING: $null discovered computers without netmask on all discovered machine\n\n" if $null;
 716  }
 717  #Try to get dns name
 718  sub _getdns{
 719    my $ip = shift;
 720    my $name;
 721    chomp(my @names = `host $ip 2>/dev/null`);
 722    for(@names){
 723     return $1 if /.+pointer (.+)/i;
 724     return $1 if /Name:\s*(\S+)/i;
 725    }
 726    return("-");
 727  }
 728  
 729  #Retrieve the name of the subnet if available
 730  sub _getnetname{
 731    my $network = shift;
 732    my $r = shift;
 733    for(@subnet){
 734        if($_->[0] eq $network){
 735        return ($_->[1], $_->[2]);
 736        last;
 737      }
 738    }
 739    return($r, $r);
 740  
 741  }
 742  
 743  #To retrieve the net ip    
 744  sub _network{
 745    my $ip = shift;
 746    my $netmask = shift;
 747    my $binip = &ip_iptobin($ip, 4);
 748    my $binmask = &ip_iptobin($netmask, 4);
 749    my $subnet = $binip & $binmask;
 750    return(&ip_bintoip($subnet, 4)) or die(Error());
 751  } 
 752  
 753  sub _bintoascii{
 754    my $binmask = shift;
 755    my $binstring = "1" x $binmask;
 756    $binstring .= "0" for(1..(32 - $binmask));
 757    
 758    return(&ip_bintoip($binstring, 4)) or die(Error());
 759  }
 760  
 761  sub _binmask{
 762    my $ip = shift;
 763    return(&ip_iptobin($ip, 4)) or die(Error());
 764  }
 765  
 766  
 767  
 768  
 769  
 770  


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1