我在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地址,只提取“ - &gt;”右侧的IP地址领域。如何轻松提取这两个文件并将它们重新组合成以下格式:
192.168.199.52->192.168.199.254
答案 0 :(得分:3)
类似的东西:
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
}