使用 Netstat 仅计算 Established、TIME_WAIT 和 Closed Wait 连接的脚本

Script that only counts Established, TIME_WAIT, and Closed Wait connections using Netstat

我正在尝试为 Windows 制作一个脚本,它只会计算系统上建立的、Time_Wait 和 Closed_Wait 连接的数量,并在命令提示符下打印它们.我已经制作了一个 shell 脚本,可以在 Linux 个盒子上执行此操作,但是 shell 个脚本在 Windows 中不起作用。我试图使用 .bat 来执行脚本,但它不起作用(可能是因为它仍然是 Windows 中的 shell 脚本 :/)它必须只显示 Established 的原因,Time_Wait 和 Closed_Wait 是因为脚本正在被监控程序使用,如果出现任何其他连接类型,该脚本将失败。谁能提出建议?谢谢!

以下(应该)在不对 *nix 和 Windows 进行任何更改的情况下工作。我已经在 Ubuntu w/ Perl v.5.18.0、Linux Mint w/ Perl v5.22.0 和 Win2k8R2(w/ Strawberry Perl v5.8.8)上进行了测试。

#!/usr/bin/perl
use strict;
use warnings;

my @stat = split '\n', `netstat -nat`;

my @wanted = qw(
                ESTABLISHED
                TIME_WAIT
                CLOSED_WAIT
                SYN_SENT
                SYN_RECV
            );

my %data = map {$_ => 0} @wanted;

for (@stat){
    s/^\s+//;

    my $status;

    if ($^O eq 'MSWin32'){
        $status = (split)[3];
    }
    else {
        $status = (split)[5];
    }

    next if ! $status;

    $data{$status}++ if defined $data{$status};
}

print "$data{$_}\n" for @wanted;