Noosrep has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have an XML file that is generated as follows:

<port protocol="tcp" portid="139"><state state="open" reason="syn-ack" + reason_ttl="64"/><service name="netbios-ssn" method="table" conf="3" +/></port> <port protocol="tcp" portid="445"><state state="open" reason="syn-ack" + reason_ttl="64"/><service name="microsoft-ds" method="table" conf="3 +"/></port> <port protocol="tcp" portid="1984"><state state="open" reason="syn-ack +" reason_ttl="64"/><service name="bigbrother" method="table" conf="3" +/></port>

With following code, I parse it:

my $fct_openPort = (join ',',$fct_host_obj->tcp_ports('open'))."\n" ; my @fct_ports = split ',', $fct_openPort; for my $fct_port (@fct_ports) { my $fct_svc = $fct_host_obj->tcp_service($fct_port); my $fct_service = $fct_svc->name() ; print "\nservice : $fct_service \n" ; print "port : $fct_port \n" ; print "service and port = $fct_port:$fct_service \n" ; }

However, the last open port has no service!

service : netbios-ssn port : 139 service and port = 139:netbios-ssn service : microsoft-ds port : 445 service and port = 445:microsoft-ds service : port : 1984 service and port = 1984 :

Does anybody have any why the last service isn't resolved? When I change

my $fct_svc = $fct_host_obj->tcp_service($fct_port);

to

my $fct_svc = $fct_host_obj->tcp_service("1984");

it does work

Replies are listed 'Best First'.
Re: Last service name comes up empty in NMAP Parser
by huck (Prior) on Jan 18, 2017 at 11:11 UTC

    To me the telling thing was that the ":" for the last was on its own line.... like there was an odd "\n".... AHHAH

    my $fct_openPort = (join ',',$fct_host_obj->tcp_ports('open'))."\n" ; my @fct_ports = split ',', $fct_openPort;
    The last member of @fct_ports is terminated with a "\n"

      Yeah, you're right.. Removing the /n did the trick. Unbelievable I didn't notice it right there
Re: Last service name comes up empty in NMAP Parser
by haukex (Archbishop) on Jan 18, 2017 at 11:16 UTC

    Hi Noosrep,

    my $fct_openPort = (join ',',$fct_host_obj->tcp_ports('open'))."\n" ; my @fct_ports = split ',', $fct_openPort;

    First of all, why first join the list of ports into a comma-separated string, only to split it back out again? Why not say for my $fct_port ($fct_host_obj->tcp_ports('open'))?

    Second, note the ."\n": you're appending a newline to the end of the string, which remains there after the split. See the Basic debugging checklist, especially item 3 (unexpected whitespace)*. Remove the ."\n" and I suspect your code will work.

    my @tcp_ports = qw/ 139 445 1984 /; my $fct_openPort = (join ',',@tcp_ports)."\n" ; my @fct_ports = split ',', $fct_openPort; use Data::Dumper; $Data::Dumper::Useqq = 1; print Dumper(\@tcp_ports,\@fct_ports); __END__ $VAR1 = [ 139, 445, 1984 ]; $VAR2 = [ 139, 445, "1984\n" ];

    Also, you never tell us which module you're using? (I assume it's not relevant here, but you never know...)

    * Update: You can already see the extra newline character in your output: "service and port = 1984 <newline> :"

    Hope this helps,
    -- Hauke D

      Module was NMAP::Parser, I thought that was clear from my question but I guess not :) Either way, you and Huck were right. Removing the \n was the solution..