【问题标题】:Get local port numbers in windows 7 [duplicate]在Windows 7中获取本地端口号[重复]
【发布时间】:2016-12-20 07:00:27
【问题描述】:

我正在尝试获取所有处于正在侦听状态的本地端口。使用

netstat -a -n

我得到以下输出:

Proto  Local Address          Foreign Address        State
TCP    0.0.0.0:8080             0.0.0.0:0              LISTENING //for example, demo data is given

但我只是不想得到端口号。

1111 //for ex, this is in listening state.

在 Windows 10 中,我可以使用

Get-NetTCPConnection -State Listen | group localport -NoElement

可行,但此命令在 Windows 7 上不可用

【问题讨论】:

    标签: windows powershell windows-7 powershell-5.0


    【解决方案1】:

    不确定是否有可用的 Windows 7 cmdlet,但您可以解析 netstat 结果:

    $objects = netstat -a -n | 
        select -Skip 4 |
        ForEach-Object {
            $line = $_ -split ' ' | Where-Object {$_ -ne ''}   
            if ($line.Count -eq 4)
            {
               New-Object -TypeName psobject -Property @{
                'Protocol'=$line[0]
                'LocalAddress'=$line[1]
                'ForeignAddress'=$line[2]
                'State'=$line[3]}
            }
        }
    

    然后您可以使用以下方式检索端口:

    $objects | Where State -eq LISTENING | Select LocalAddress | Foreach { 
        $_ -replace '.*:(\d+).*', '$1' 
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-17
      • 1970-01-01
      • 2018-10-31
      • 2012-06-28
      • 2012-06-12
      • 1970-01-01
      • 2012-05-08
      • 2019-08-14
      • 1970-01-01
      相关资源
      最近更新 更多