..

统计 tcp 连接数

直观看到服务器上各个进程链接 tcp 数量的情况

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;
}

执行结果

hellojukay@local ~ $ sudo perl ss.pl
pid  tcp_count   cmdline
15026     1     /usr/lib/i386-linux-gnu/deepin-wine/./wineserver.real-p0
 7907     2     /usr/sbin/vmware-authdlauncher
24113     1     /usr/sbin/smbd
 9275    13     /opt/google/chrome/chrome --type=utility --field-trial-handle=2182854705504331656,15972359820735917946,131072 --lang=zh-CN --service-sandbox-type=network --disable-webrtc-apm-in-audio-service --shared-files
 8150     3     /usr/lib/vmware/bin/vmware-hostd-a/etc/vmware/hostd/config.xml
  916     1     sshroot@10.44.155.5
 7835     4     /usr/sbin/smbd
24106     1     /usr/lib/gvfs/gvfsd-smb-browse--spawner:1.12/org/gtk/gvfs/exec_spaw/3
 6653     2     /usr/sbin/sshd-D
 8881     1     /opt/lanxin/bin/LxMainNew
24198     1     /opt/kingsoft/wps-office/office6/wpsoffice/qingbangong/start_from=qingipcautologin
 5459     2     /usr/sbin/cupsd-l
 6756     2     nginx: worker process