1

I would like to get netstat to not display port numbers on the foreign address so I can run some statistics on it. This is for a FreeBSD system.

The following is a example of the output.

<root>:/# netstat -an | grep .80 |head
tcp4       0      0  61.129.65.176.80      123.120.207.172.51972   ESTABLISHED
tcp4     491      0  61.129.65.176.80      171.250.180.211.51000  ESTABLISHED
tcp4     286      0  61.129.65.176.80      123.120.207.17210399     ESTABLISHED
tcp4     299      0  61.129.65.176.80      211.8.128.46.35458    ESTABLISHED
tcp4       0      0  61.129.65.176.80      123.139.147.112.62778    ESTABLISHED
tcp4     361      0  61.129.65.176.80      239.187.139.47.17607  ESTABLISHED
tcp4     509      0  61.129.65.176.80      74.74.87.36.7822   ESTABLISHED
tcp4     324      0  61.129.65.176.80      75.30.126.198.60721     ESTABLISHED
tcp4     508      0  61.129.65.176.80      149.78.116.66.12120   ESTABLISHED
tcp4     321      0  61.129.65.176.80      48.150.75.171.2617     ESTABLISHED
<root>:/# 
  • 1
    You can filter your output using a tool like sed, awk, or cut. Why not post some sample netstat raw output, and then an example of what you want to see? That will help others understand more precisely what you need. – mrb Aug 28 '12 at 13:42
1

Add this sed command at the end of your pipe. It does a greeding search until last . and delete it and all digits that follow it.

... | sed -e 's/^\(.*\)\.[0-9]*/\1/'

It yields:

tcp4       0      0  61.129.65.176.80      123.120.207.172   ESTABLISHED
tcp4     491      0  61.129.65.176.80      171.250.180.211  ESTABLISHED
tcp4     286      0  61.129.65.176.80      123.120.207.172     ESTABLISHED
tcp4     299      0  61.129.65.176.80      211.8.128.46    ESTABLISHED
tcp4       0      0  61.129.65.176.80      123.139.147.112    ESTABLISHED
tcp4     361      0  61.129.65.176.80      239.187.139.47  ESTABLISHED
tcp4     509      0  61.129.65.176.80      74.74.87.36   ESTABLISHED
tcp4     324      0  61.129.65.176.80      75.30.126.198     ESTABLISHED
tcp4     508      0  61.129.65.176.80      149.78.116.66   ESTABLISHED
tcp4     321      0  61.129.65.176.80      48.150.75.171     ESTABLISHED

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.