Skip to content

Instantly share code, notes, and snippets.

@ramiroencinas
Created December 21, 2020 19:28
Show Gist options
  • Save ramiroencinas/6b83f02df3bd4459a0c0d62e319c9ba2 to your computer and use it in GitHub Desktop.
Save ramiroencinas/6b83f02df3bd4459a0c0d62e319c9ba2 to your computer and use it in GitHub Desktop.
use POSIX qw(strftime);
my $date, $remote_ip, $remote_port, $user, $group, $cmdpath;
while (1) {
foreach (`ss -tupn`) {
$date = strftime "%d-%m-%Y %H:%M:%S", localtime;
if ($_ =~ /(\d+\.\d+\.\d+\.\d+):(\d+)\s+users.*?pid=(\d+)/) {
$remote_ip = $1;
$remote_port = $2;
$user = `stat --printf="%U" /proc/$3/exe`;
$group = `stat --printf="%G" /proc/$3/exe`;
$cmdpath = `stat --printf="%N" /proc/$3/exe`;
$cmdpath =~ s/^.*?-> '//;
$cmdpath =~ s/'//;
print "$date $user $group $cmdpath $remote_ip $remote_port\n";
}
}
sleep 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment