2010-10-21

Perl one-liner: a process is communicating with which hosts (using lsof)?

You want to know which hosts that a process is communicating with. You use lsof. You want a Perl one-liner to parse the output nicely for you.

In the below example, the PID of the process I am observing is 1114114.

lsof -p 1114114 -a -i TCP -P | perl -ane '$h{(split(":", (split("->", $F[-2]))[-1]))[0]}++ if /ESTABLISHED/; END {for $i (keys %h) {print "$i = $h{$i}\n";}}'

Example output:

bob.example.com = 6
lemon.example.com = 12
smart100.example.com = 60
smart101.example.com = 8
windows.example.com = 2

No comments:

Post a Comment