【问题标题】:Opening up a port with powershell用powershell打开一个端口
【发布时间】:2012-10-19 04:56:54
【问题描述】:

Machine A 上,我正在运行端口扫描程序。在Machine B 我想以有组织的方式打开和关闭端口。我希望通过 powershell 完成所有这些工作。

我发现THIS 脚本可以在Machine B 上运行,但是当从Machine A 扫描同一端口时,它仍然说它已关闭。

你们有谁知道我如何在Machine B 上成功打开一个端口

【问题讨论】:

  • 如果您描述Machine AMachine B 之间的关系可能会有所帮助(这里讨论网络层次结构)。
  • Machine A 是 10.0.0.20 和 Machine B 是 10.0.0.199 它们在同一个域中。这是否解释了你在 sorta 得到了什么?
  • 它确实让生活更简单。机器之间的距离越远,在排除故障原因时必须考虑的因素就越多。检查我更新的答案。

标签: powershell port


【解决方案1】:

尽可能避免使用 COM。你可以使用TcpListener打开一个端口:

$Listener = [System.Net.Sockets.TcpListener]9999;
$Listener.Start();
#wait, try connect from another PC etc.
$Listener.Stop();

如果您在调试期间碰巧错过了Stop 命令 - 只需关闭并重新打开您打开套接字的应用程序 - 应该清除挂起的端口。就我而言,它是PowerGUI script editor

然后使用TcpClient查看。

(new-object Net.Sockets.TcpClient).Connect($host, $port)

如果无法连接,则表示防火墙正在阻止它。

编辑:要在收到连接时打印一条消息,您应该能够使用此代码(基于this article from MSDN):

#put this code in between Start and Stop calls.
while($true) 
{
    $client = $Listener.AcceptTcpClient();
    Write-Host "Connected!";
    $client.Close();
}

【讨论】:

  • 我的目标是不必接触防火墙。意思是,我需要能够打开和关闭机器上端口的软件,这样,如果我测试端口并被拒绝,我知道这是由于防火墙,而不是机器
  • 太好了,我一直在使用 tcpClient。我刚刚发现子网中的所有端口都是打开的。所以一个更好的例子是Machine A 是 10.0.0.199 而Machine B 是 192.168.1.10
  • @Neolisk 太棒了!感谢您在 3 年后更新:)
  • @Neolisk Genial!格拉西亚斯
  • 也适用于 Linux (SLES 12 SP 3)。谢谢!
【解决方案2】:

我需要一些东西,它不仅会承认端口是打开的,而且还会做出响应。所以,这是我的超级基本的 not-quite-telnet 服务器。

Clear-Host; $VerbosePreference="Continue"; $Port=23
$EndPoint=[System.Net.IPEndPoint]::new([System.Net.IPAddress]::Parse("<ip address>"),$Port)
$Listener=[System.Net.Sockets.TcpListener]::new($EndPoint)

$KeepListening=$true
while ($KeepListening) {
  $Listener.Start()
  while (!$Listener.Pending) { Start-Sleep -Milliseconds 100 }

  $Client=$Listener.AcceptTcpClient()
  Write-Output "Incoming connection logged from $($Client.Client.RemoteEndPoint.Address):$($Client.Client.RemoteEndPoint.Port)"

  $Stream=$Client.GetStream()
  $Timer=10; $Ticks=0; $Continue=$true
  $Response=[System.Text.Encoding]::UTF8.GetBytes("I see you.  I will die in $($Timer.ToString()) seconds.`r`nHit <space> to add another 10 seconds.`r`nType q to quit now.`r`nType x to terminate listener.`r`n`r`n")
  $Stream.Write($Response,0,$Response.Length)

  $StartTimer=(Get-Date).Ticks
  while (($Timer -gt 0)  -and $Continue) {
    if ($Stream.DataAvailable) {
      $Buffer=$Stream.ReadByte()
      Write-Output "Received Data: $($Buffer.ToString())"
      if ($Buffer -eq 113) {
        $Continue=$false
        $Response=[System.Text.Encoding]::UTF8.GetBytes("`r`nI am terminating this session.  Bye!`r`n")
      }
      elseif ($Buffer -eq 32) {
        $Timer+=10
        $Response=[System.Text.Encoding]::UTF8.GetBytes("`r`nAdding another 10 seconds.`r`nI will die in $($Timer.ToString()) seconds.`r`n")
      }
      elseif ($Buffer -eq 120) {
        $Continue=$false
        $KeepListening=$false
        $Response=[System.Text.Encoding]::UTF8.GetBytes("`r`nI am terminating the listener.  :-(`r`n")
      }
      else { $Response=[System.Text.Encoding]::UTF8.GetBytes("`r`nI see you.  I will die in $($Timer.ToString()) seconds.`r`nHit <space> to add another 10 seconds.`r`nType q to quit this session.`r`nType x to terminate listener.`r`n`r`n") }

      $Stream.Write($Response,0,$Response.Length)
    }
    $EndTimer=(Get-Date).Ticks
    $Ticks=$EndTimer-$StartTimer
    if ($Ticks -gt 10000000) { $Timer--; $StartTimer=(Get-Date).Ticks }
  }

  $Client.Close()
}
$Listener.Stop()

【讨论】:

    猜你喜欢
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多