Linux lsof 需要帮助解析输出

Linux lsof Need help parsing output

我在 linux 系统上使用以下命令:

lsof -i -n | egrep '\<ssh\>'|awk '{print ,}'

它产生这样的输出:

192.168.199.52:ssh->192.168.199.254:17598 (ESTABLISHED)
192.168.199.52:ssh->192.168.199.254:17598 (ESTABLISHED)
192.168.199.52:56448->69.168.130.22:ssh (ESTABLISHED)
192.168.199.52:56449->69.168.130.22:ssh (ESTABLISHED)
192.168.199.52:56454->69.168.130.22:ssh (ESTABLISHED)
192.168.199.52:56458->69.168.130.22:ssh (ESTABLISHED)
192.168.199.52:56460->69.168.130.22:ssh (ESTABLISHED)
192.168.199.52:56468->69.168.130.22:ssh (ESTABLISHED)
192.168.199.52:ssh->192.168.199.254:56671 (ESTABLISHED)
192.168.199.52:ssh->192.168.199.254:56671 (ESTABLISHED)
192.168.199.52:ssh->192.168.199.254:56672 (ESTABLISHED)

我想只提取左侧的 IP 地址和“->”字段右侧的 IP 地址。我怎样才能轻松提取这两个文件并将它们重新组合成以下格式:

192.168.199.52->192.168.199.254

类似于:

lsof -i -n | awk ' ~ /:ssh(-|$)/{ gsub(/:[^-]*/, "", ); print  }'

或者用 8 美元代替 9 美元。

awk 命令详情:

 ~ /:ssh(-|$)/ {           # when ":ssh" is at the end of field 9 or
                             # followed by an hyphen
    gsub(/:[^-]*/, "", );  # remove all the semi-colon followed by characters that
                             # are not an hyphen from the field 9
    print                  # and print it
}