linux,统计端口监听情况

21 阅读1分钟

查看所有的端口监听

ss -tulpn | grep "LISTEN"

汇总,并且过滤掉,只允许本机访问的端口

端口显示
ss -tulpn | awk '$2=="LISTEN" && $5 !~ /^127\.0\.0\.1|^\[?::1\]?/ {
    gsub(/\[|\]/, "", $5);
    split($5, a, ":");
    port = a[length(a)];
    ports[port] = 1
} 
END {
    n = asorti(ports, sorted);
    for(i=1; i<=n; i++) {
        if(i>1) printf ",";
        printf "%s", sorted[i]
    }
    print ""
}'

列表展示
ss -tulpn | awk '$2=="LISTEN" && $5 !~ /^127\.0\.0\.1|^\[?::1\]?/ {
    print $5
}'