Linux lsof需要帮助来解析输出

里克

我在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
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章