programing

파워셸을 사용하여 포트 열기

oldcodes 2023. 10. 11. 20:58
반응형

파워셸을 사용하여 포트 열기

Machine A포트 스캐너를 실행하고 있습니다. 켜짐Machine B저는 조직적인 방식으로 항구를 열고 닫고 싶습니다.저는 파워셸을 통해 이 모든 것을 해내고 싶습니다.

스크립트에서 실행할 수 있는 것을 찾았습니다.Machine B그러나 동일한 포트를 검색할 때Machine A아직도 문을 닫았다고 합니다.

포트를 성공적으로 열 수 있는 방법을 아는 사람이 있습니까?Machine B

가능하면 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();
}

항구가 열려 있다는 것을 인정할 뿐만 아니라 대응할 수 있는 것이 필요했습니다.자, 이것이 저의 초기본적인 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()

언급URL : https://stackoverflow.com/questions/13129060/opening-up-a-port-with-powershell

반응형