I have generated an XML file with following command: nmap -PE -oX - $range > /home/users/axiroot/scripts/monitor-client/tmp/nmap.xml and parse it with following code:
my @IPsinrange = &getAllIPs($range) ; my $np = new Nmap::Parser; $np->parsefile('/home/users/axiroot/scripts/monitor-client/tmp/nmap.xm +l'); #using filenames my $si = $np->get_session(); #my @MACsinrange = &getAllMACs($range,) foreach my $ip (@IPsinrange) { $output .= "<tr>\n" ; $output .= "<th>$ip</th>\n" ; for my $host_obj ($np->all_hosts()){ if ( $host_obj->ipv4_addr() eq $ip ){ my $state = ""; $state = $host_obj->status() ; if ( $state eq "up" ) { $output .= "<td align='center'>$state</td>\n" ; + my $hostname = $host_obj->hostname() ; if ( $hostname eq 0 ) { $hostname = "Hostname niet gekend" ; } #print "hostname: $hostname \n" ; $output .= "<td align='center'>$hostname</td>\n" ; my $a = nslookup(host => $ip, type => "PTR"); if ( $a eq ""){ $output .= "<td align='center'>Niet in DNS</td>\n" ; + } else { $output .= "<td align='center'>$a</td>\n" ; + } my $openPort = (join ',',$host_obj->tcp_ports('open')) +."\n" ; my @ports = split ',', $openPort; $output .= "<td align='left'>"; foreach my $port (@ports) { my $svc = $host_obj->tcp_service($port); my $service = $svc->name() ; #$output .= "$port : $service </br>" ; $output .= "$port : $service </br>" ; } $output .= "</td>\n" ; my $mac = $host_obj->mac_addr() ; $output .= "<td align='center'>$mac</td>\n" ; + my $type = $host_obj->mac_vendor() ; $output .= "<td align='center'>$type</td>\n" ; + } } } $output .= "<tr>\n" ; }
When I delete the section of tcp ports, the script runs without any speed problems. However, as soon as I want to parse the tcp ports output, the script takes very long. For instance a range of 4 addresses takes up to 20 minutes for the script to complete. That's doable but with a /24, this is simply too long. Now I have read that this kind of nmap port scanning takes a long time but is there something wrong with my code that makes it very slow? Perl newbie here so I think that could be the case :)

In reply to NMAP Parser very slow by Noosrep

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.