gpt4 book ai didi

Linux lsof 需要帮助解析输出

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:51:08 25 4
gpt4 key购买 nike

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

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

它产生这样的输出:

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 '$9 ~ /:ssh(-|$)/{ gsub(/:[^-]*/, "", $9); print $9 }'

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

awk 命令详细信息:

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

关于Linux lsof 需要帮助解析输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31248455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com