1820561 Members
2429 Online
109626 Solutions
New Discussion юеВ

Re: netstat -a | grep

 
SM_3
Super Advisor

netstat -a | grep

Hello

I want to netstat -a | grep '23' (or any port number).

I dont want output to contain
6523 or
6723

I want output to contain
23 or
25 etc

Thanks.




11 REPLIES 11
Steven E. Protter
Exalted Contributor

Re: netstat -a | grep

Shalom,

try:

netstat -a | grep " 23"

or

netstat -a | grep ":23"

putting a space in the quotes gets rid of 6523

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Arunvijai_4
Honored Contributor

Re: netstat -a | grep

Hi,

You can restrict by

# netstat -a |grep "23"

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Kofi ARTHIABAH
Honored Contributor

Re: netstat -a | grep

How about:

netstat -an | grep '.23 '

note the -an says do not perform a name lookup (otherwise, 23 will be translated to telnet and will never show up) and note that there is a period before the 23 and a space after the 23.

you might also want to consider using lsof (download it if you dont already have it)

lsof -i :23

Hope this helps.

Kofi
nothing wrong with me that a few lines of code cannot fix!
Jonathan Fife
Honored Contributor

Re: netstat -a | grep

Hi,

If you do a grep -w 23 it will search for 23 as a word by itself.

netstat -a | grep -w 23

HTH
Decay is inherent in all compounded things. Strive on with diligence
Marcel Boogert_1
Trusted Contributor

Re: netstat -a | grep

Hi,

Use: netstat -a | grep ".23"

Regards,
MB.
James R. Ferguson
Acclaimed Contributor

Re: netstat -a | grep

Hi:

# netstat -a|perl -nae 'print if $F[3]=~/\.23/'

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: netstat -a | grep

You probably really want netstat -an so that all ports are listed numerically rather than symbolically and because the output is IP_address.portnumber we can take advantage of that "." and use grep -E (enhanced grep).

netstat -an | grep -E -e '\.23 '

Note the '\.' rather than '.'. In Regular Expressions, '.' represents any character so we need to explicitly look for the '.' and thus we need to escape the '.' with a backslash.
If it ain't broke, I can fix that.
spex
Honored Contributor

Re: netstat -a | grep

Hi,

# netstat -an | grep -E '.+\..+\..+\..+\.23 |\*.23 '

PCS
Jonathan Fife
Honored Contributor

Re: netstat -a | grep

Just a note -- grep ".23" won't work since grep will interepret . as "any char" per regexp syntax.

If you escape the . (ie. grep "\.23" that will make grep search for a period, but will still include ports that start with 23 (ie. 2301, 239, etc.)
Decay is inherent in all compounded things. Strive on with diligence
SM_3
Super Advisor

Re: netstat -a | grep

Blimey, that was quick.


Thanks.


SM_3
Super Advisor

Re: netstat -a | grep

apologies for the delayed points.