【问题标题】:How to print the IP and the Port separately with netstat?如何用netstat分别打印IP和端口?
【发布时间】:2015-05-31 18:56:03
【问题描述】:

我想用netstat命令分别打印IP和端口,

我试过这个:

netstat -nat | awk '{print $4}'

但它告诉我:

192.168.1.213:40405

我想要这样的东西:

首先是IP:192.168.1.213

使用另一个命令端口:40405

【问题讨论】:

    标签: linux shell netstat


    【解决方案1】:

    您可以随时将其通过管道传输到 cut:

    # Just the IP:
    $ netstat -nat | awk '{print $4}' | cut -d ":" -f1
    
    # Just the port:
    $ netstat -nat | awk '{print $4}' | cut -d ":" -f2
    

    【讨论】:

      【解决方案2】:

      如果你希望它们作为不同的命令,你可以使用 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##*:}
      

      【讨论】:

        【解决方案3】:

        我正在使用 zsh shell,我正在使用相同的命令在新行中获取端口

        netstat -nat | awk '{print $4}'
        

        也许可以尝试更改您的个人资料偏好

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-05-19
          • 2019-10-29
          • 2021-09-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多