【发布时间】:2015-05-31 18:56:03
【问题描述】:
我想用netstat命令分别打印IP和端口,
我试过这个:
netstat -nat | awk '{print $4}'
但它告诉我:
192.168.1.213:40405
我想要这样的东西:
首先是IP:192.168.1.213
使用另一个命令端口:40405
【问题讨论】:
我想用netstat命令分别打印IP和端口,
我试过这个:
netstat -nat | awk '{print $4}'
但它告诉我:
192.168.1.213:40405
我想要这样的东西:
首先是IP:192.168.1.213
使用另一个命令端口:40405
【问题讨论】:
您可以随时将其通过管道传输到 cut:
# Just the IP:
$ netstat -nat | awk '{print $4}' | cut -d ":" -f1
# Just the port:
$ netstat -nat | awk '{print $4}' | cut -d ":" -f2
【讨论】:
如果你希望它们作为不同的命令,你可以使用 sed 来做:
netstat -nat | awk '{print $4}' | sed -e 's/:.*//' # gives IP only
netstat -nat | awk '{print $4}' | sed -e 's/.*://' # gives port only
根据您的使用方式,您可以将其存储在 bash 变量中,并在访问时完成相同的操作
both=$(netstat -nat | awk '{print $4}')
ip=${both%%:*}
port=${both##*:}
【讨论】:
我正在使用 zsh shell,我正在使用相同的命令在新行中获取端口
netstat -nat | awk '{print $4}'
也许可以尝试更改您的个人资料偏好
【讨论】: