Hello,
im trying write a little code that checks if a open is open but it do not seem to work for me, i hope you can help
#!/usr/contrib/bin/perl -w $portnum=$ARGV[0]; @netstat=`netstat -an`; @foundlines = grep /^\d+.\d+.\d+.\d+.$portnum/, @netstat; foreach $port(@foundlines){ if ( $port =~ /(\d+.\d+.\d+.\d+.$portnum) (\d+.\d+.\d+.\d+.\d+)\s+\ +d+\s+\d\s+\d+\s+\d\s+(\w+)$/){ if ($3 ne "LISTEN" or $port ne "ESTABLISHED" ){ print "$1 $3\n"; }else{ print "ok"; } } }

The problem is if i pick a number like "94" it get also "9494" BUT cuts off the first part of the ip-addr, so : 228.132.245.9494 So how i can define make sure it: 1) only picks the ones that matches precisely the port nummer? 2) and why do it cut of the first part of the ip-addr ?

the std output from a "netstat -an|grep 949"

131.228.132.245.9494 131.228.132.159.41713 24820 0 8760 0 T +IME_WAIT 131.228.132.245.94 131.228.132.159.41703 24820 0 8760 0 TIM +E_WAIT

The funny part ???

root@toybox: perl portstat.pl 94 228.132.245.9494 TIME_WAIT

this looks right tho

root@toybox: perl portstat.pl 9494 131.228.132.245.9494 TIME_WAIT

Here is what i want:

root@toybox: perl portstat.pl 9494 131.228.132.245.94 TIME_WAIT

EDIT::: Pasted the complete code and added some more stuff... This is really giving me a headache. EDIT:::

Thanks alot for the help.
,Miller

janitored by ybiC: Retitle from "Checking status of ports" for better searching, minor format cleanup for legibility


In reply to Regex to parse netstat output by miller

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.