I have the following code - which strings together nmap, fuser, and ps and gives me pretty output. The problem I have is that some subscripts of @pid array contain a list instead of a scalar so when it prints I get strangely formatted output.
use strict; # Variables my @nmap; my @port; my @protocol; my @pid; my @app; my $junk; my $i; print "\nScanning for open ports... "; chomp(@nmap = `nmap localhost`); for (1..5) { shift(@nmap); } for (1..2) { pop(@nmap); } $i = 0; foreach (@nmap) { if (! @nmap[$i] =~ /^\s*$/) { (@port[$i], @protocol[$i]) = split /\//, $_; @protocol[$i] = split / /, @protocol[$i]; @pid[$i] = `fuser -n @protocol[$i] @port[$i]`; chomp(($junk, @pid[$i]) = split /:\s+/,@pid[$i]); @app[$i] = `/bin/ps --no-headers -o %c @pid[$i]`; $i++; } } print "done.\n\n"; print "The following ports are open on your system\n\n"; printf "%8s%12s%12s%15s\n","Port", "Protocol", "PID", "App"; print "-----------------------------------------------\n"; $i = 0; foreach (@nmap) { printf "%8s%12s%12s%16s\n", @port[$i], @protocol[$i], @pid[$i][1], + @app[$i]; $i++; }
good looking output where there is only one PID per port:
Scanning for open ports... done. The following ports are open on your system Port Protocol PID App ----------------------------------------------- 22 tcp 21232 sshd 515 tcp 21943 lpd 6000 tcp 1738 X
bad looking output with multiple PIDs per port:
Scanning for open ports... done. The following ports are open on your system Port Protocol PID App ----------------------------------------------- 25 tcp 198 exim 53 tcp 226 named 139 tcp242 3995 3997 4007 4009 4011 smbd 143 tcp 269 1006 xinetd
I think I know what I want to do but I don't know how to do it. I want to print the first item in the @pid$i list and then have a foreach loop to print the rest after the first line.

In reply to @pid = a list - need a way to print by Hayl_

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.