21

Machine A私はポートスキャナーを実行しています。整理Machine Bされた方法でポートを開閉したいと思います。私はこれらすべてをPowerShellを介して実行したいと思っています。

このスクリプトが実行されるのを見つけましMachine Bたが、同じポートをスキャンすると、Machine Aまだ閉じていると表示されます。

でポートを正常に開く方法を知っている人はいますかMachine B

4

2 に答える 2

42

可能であれば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)

接続できない場合は、ファイアウォールがブロックしていることを意味します。

編集:接続が受信されたときにメッセージを印刷するには、次のコードを使用できる必要があります(MSDNのこの記事に基づく):

#put this code in between Start and Stop calls.
while($true) 
{
    $client = $Listener.AcceptTcpClient();
    Write-Host "Connected!";
    $client.Close();
}
于 2012-10-29T20:30:24.403 に答える
1

ポートが開いていることを確認するだけでなく、応答するものが必要でした。それで、これが私の超基本的な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()
于 2020-12-04T17:07:48.453 に答える