当我运行netstat时,我有以下输出:
$ netstat -lnp --inet
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:51413 0.0.0.0:* LISTEN 6155/transmission-g
tcp 0 0 127.0.0.1:56424 0.0.0.0:* LISTEN 450/weechat
netstat: no support for `AF INET (sctp)' on this system.我想要只使用bash的端口列表(使用python会更容易)。我有这个:
$ netstat -lnp --inet | cut -d' ' -f45-50 | sed 's/[^0-9]*//g'
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
netstat: no support for `AF INET (sctp)' on this system.
6155
450但我无法摆脱这段文字。有什么帮助吗?
发布于 2014-11-18 20:03:10
台词:
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)被发送到stderr而不是stdout,因此您可以通过将stderr发送到/dev/null而不是让它在控制台中打印来消除它们。
如:
$ netstat -lnp --inet 2>/dev/null| cut -d' ' -f45-50 | sed 's/[^0-9]*//g'顺便说一句,通过egrep "\w“的管道将消除空线。
$ netstat -lnp --inet 2>/dev/null| cut -d' ' -f45-50 | sed 's/[^0-9]*//g' | egrep "\w"发布于 2014-11-18 20:08:49
使用awk更健壮:
netstat -lnp --inet | awk -F'LISTEN *|/' '/^(tcp|udp)/{print $2}' file
6155
450发布于 2014-11-18 20:02:54
尝试:
netstat -lnp --inet | grep -E 'tcp|udp' | cut -d' ' -f45-50 | sed 's/[^0-9]*//g' https://stackoverflow.com/questions/27002915
复制相似问题