Skip to content

Instantly share code, notes, and snippets.

@hellojukay
Created June 15, 2020 09:29
Show Gist options
  • Save hellojukay/3b724671ddfd6648b51c9b4bca21bc5f to your computer and use it in GitHub Desktop.
Save hellojukay/3b724671ddfd6648b51c9b4bca21bc5f to your computer and use it in GitHub Desktop.
count tcp connection by process, base on ss command line
use strict;
use warnings;
my @lines = `ss -atp`;
my %hash;
for my $line (@lines) {
if($line =~ /pid=(\d{1,9})/) {
$hash{$1}++;
}
}
printf "%s %10s %s\n", "pid","tcp_count","cmdline";
for my $pid (keys %hash) {
my $cmd = `cat /proc/$pid/cmdline`;
printf "%5d %5d %s\n", $pid,$hash{$pid},$cmd;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment