在Perl或Java端口上运行的应用程序的名称

Xet*_*ius 2 java windows perl networking

Xampp附带了一个名为xampp-portcheck.exe的简洁可执行文件.如果所需端口是空闲的,则响应,如果不是,则在这些端口上运行哪些应用程序.

我可以通过访问netstat详细信息来检查端口上是否正在运行某些内容,但是如何找到在Windows中的端口上运行的应用程序?

Sin*_*nür 5

的CPAN模块的Win32 :: IPHelper提供访问GetExtendedTcpTable,其提供ProcessID用于每个连接.

Win32 :: Process :: Info提供有关所有正在运行的进程的信息.

结合这两者,我们得到:

#!/usr/bin/perl

use strict;
use warnings;

use Win32;
use Win32::API;
use Win32::IPHelper;
use Win32::Process::Info qw( NT );

use Data::Dumper;

my @tcptable;

Win32::IPHelper::GetExtendedTcpTable(\@tcptable, 1);

my $pi = Win32::Process::Info->new;
my %pinfo = map {$_->{ProcessId} => $_ } $pi->GetProcInfo;

for my $conn ( @tcptable ) {
    my $pid = $conn->{ProcessId};
    $conn->{ProcessName} = $pinfo{$pid}->{Name};
    $conn->{ProcessExecutablePath} = $pinfo{$pid}->{ExecutablePath};
}

@tcptable =
    sort { $a->[0] cmp $b->[0] }
    map  {[ sprintf("%s:%s", $_->{LocalAddr}, $_->{LocalPort}) => $_ ]}
    @tcptable;

print Dumper \@tcptable;
Run Code Online (Sandbox Code Playgroud)

输出:

  [
    '0.0.0.0:135',
    {
      'RemotePort' => 0,
      'LocalPort' => 135,
      'LocalAddr' => '0.0.0.0',
      'State' => 'LISTENING',
      'ProcessId' => 1836,
      'ProcessName' => 'svchost.exe',
      'ProcessExecutablePath' => 'C:\\WINDOWS\\system32\\svchost.exe',
      'RemoteAddr' => '0.0.0.0'
    }
  ],
  ...
  [
    '192.168.169.150:1841',
    {
      'RemotePort' => 80,
      'LocalPort' => 1841,
      'LocalAddr' => '192.168.169.150',
      'State' => 'ESTABLISHED',
      'ProcessId' => 1868,
      'ProcessName' => 'firefox.exe',
      'ProcessExecutablePath' => 'C:\\Program Files\\Mozilla Firefox\\firefox.exe',
      'RemoteAddr' => '69.59.196.211'
    }
  ],
Run Code Online (Sandbox Code Playgroud)

Phewwww连接所有这些点令人筋疲力尽.