Created
June 15, 2020 09:29
-
-
Save hellojukay/3b724671ddfd6648b51c9b4bca21bc5f to your computer and use it in GitHub Desktop.
count tcp connection by process, base on ss command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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