如何用netstat分别打印IP和端口?
How to print the IP and the Port separately with netstat?
我想使用 netstat
命令分别打印 IP 和端口,
我试过这个:
netstat -nat | awk '{print }'
但它给了我:
192.168.1.213:40405
我想要这样的东西:
首先是IP:192.168.1.213
并使用另一个命令端口:40405
您总是可以将其通过管道传输到 cut
:
# Just the IP:
$ netstat -nat | awk '{print }' | cut -d ":" -f1
# Just the port:
$ netstat -nat | awk '{print }' | cut -d ":" -f2
如果您希望它们作为不同的命令,您可以像这样使用 sed:
netstat -nat | awk '{print }' | sed -e 's/:.*//' # gives IP only
netstat -nat | awk '{print }' | sed -e 's/.*://' # gives port only
根据您的使用方式,您可以将它存储在一个 bash 变量中,并在像
那样访问它时完成同样的事情
both=$(netstat -nat | awk '{print }')
ip=${both%%:*}
port=${both##*:}
我正在使用 zsh shell 并且我正在使用相同的命令在新行中获取端口
netstat -nat | awk '{print }'
也许可以尝试更改您的个人资料首选项
我想使用 netstat
命令分别打印 IP 和端口,
我试过这个:
netstat -nat | awk '{print }'
但它给了我:
192.168.1.213:40405
我想要这样的东西:
首先是IP:192.168.1.213
并使用另一个命令端口:40405
您总是可以将其通过管道传输到 cut
:
# Just the IP:
$ netstat -nat | awk '{print }' | cut -d ":" -f1
# Just the port:
$ netstat -nat | awk '{print }' | cut -d ":" -f2
如果您希望它们作为不同的命令,您可以像这样使用 sed:
netstat -nat | awk '{print }' | sed -e 's/:.*//' # gives IP only
netstat -nat | awk '{print }' | sed -e 's/.*://' # gives port only
根据您的使用方式,您可以将它存储在一个 bash 变量中,并在像
那样访问它时完成同样的事情both=$(netstat -nat | awk '{print }')
ip=${both%%:*}
port=${both##*:}
我正在使用 zsh shell 并且我正在使用相同的命令在新行中获取端口
netstat -nat | awk '{print }'
也许可以尝试更改您的个人资料首选项