Command-Line

讓 netstat 不顯示外部地址的埠號

  • August 28, 2012

我想讓 netstat 不在外國地址上顯示埠號,這樣我就可以對其進行一些統計。這是針對 FreeBSD 系統的。

以下是輸出範例。

<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>:/# 

sed在管道的末尾添加此命令。它會進行貪婪搜尋,直到最後.並刪除它以及它後面的所有數字。

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

它產生:

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

引用自:https://unix.stackexchange.com/questions/46508