miller has asked for the wisdom of the Perl Monks concerning the following question:
#!/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. janitored by ybiC: Retitle from "Checking status of ports" for better searching, minor format cleanup for legibility
,Miller
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex to parse netstat output
by Abigail-II (Bishop) on Feb 04, 2004 at 10:26 UTC | |
|
Re: Regex to parse netstat output
by ysth (Canon) on Feb 04, 2004 at 11:23 UTC | |
|
Re: Regex to parse netstat output
by ysth (Canon) on Feb 04, 2004 at 10:25 UTC | |
|
Re: Regex to parse netstat output
by z3d (Scribe) on Feb 04, 2004 at 14:07 UTC | |
|
Re: Regex to parse netstat output
by sedusedan (Pilgrim) on Oct 04, 2012 at 09:12 UTC |