Posted On July 28, 2021

PowerShell: How To Test A Server Ephemeral Port

kimconnect 0 comments
blog.KimConnect.com >> Codes , Networking >> PowerShell: How To Test A Server Ephemeral Port
# Setup a listening port on server
# This session will automatically terminates after a number of test counts

function initTestEpheralPort{
  param(
    $port=59848,
    $testCount=3
  )
  $cmdlet="(new-object Net.Sockets.TcpClient).Connect('$env:computername', $port)"
  write-host "$env:computername is now listening on port $port"
  write-host "Please run this function at the client side:`r`n$cmdlet"
  $listener=[System.Net.Sockets.TcpListener]$port;
  $listener.Start();
  while($testCount) 
  {    
      $clientAccepted=$listener.AcceptTcpClient();
      if($clientAccepted){
          write-host "$((netstat -ano -p tcp|select-string "$port"|out-string))"
          $testCount--;
          Write-Host "Connection test $testCount remains!";   
      }
      $clientAccepted.Close();
      if(!$testCount){
          # Stop listening on the server
          $listener.Stop();
          write-host "Tests have completed."  
      }
  }
}

initTestEpheralPort 59848
# Test reachability from a client machine
$server='nameOrIpHere'
$port=59848
(new-object Net.Sockets.TcpClient).Connect($server, $port)

Sample Output:

[SERVER1]: PS C:\Users\kdoan\Documents> initTestEpheralPort 59848
SERVER1 is now listening on port 59848
Please run this function at the client side:
(new-object Net.Sockets.TcpClient).Connect('SERVER1', 59848)

  TCP    0.0.0.0:59848          0.0.0.0:0              LISTENING       45568
  TCP    x.x.x.x:59848    y.y.y.y:50997     ESTABLISHED     45568



Connection test 2 remains!

  TCP    0.0.0.0:59848          0.0.0.0:0              LISTENING       45568
  TCP    x.x.x.x:59848    y.y.y.y:50997     FIN_WAIT_2      45568
  TCP    x.x.x.x:59848    y.y.y.y:51021     ESTABLISHED     45568



Connection test 1 remains!

  TCP    0.0.0.0:59848          0.0.0.0:0              LISTENING       45568
  TCP    x.x.x.x:59848    y.y.y.y:50997     FIN_WAIT_2      45568
  TCP    x.x.x.x:59848    y.y.y.y:51021     FIN_WAIT_2      45568
  TCP    x.x.x.x:59848    y.y.y.y:51040     ESTABLISHED     45568

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Repair Windows Server 2016 Error 0x800f081f

Run this function in PowerShell as Administrator: function resetWindowsUpdate{ # Source: https://learn.microsoft.com/en-us/troubleshoot/windows-client/installing-updates-features-roles/additional-resources-for-windows-update write-host 'Resetting Windows…

PowerShell: Check Whether an Application Is Installed Using Known Service Name

# Check whether product is installed $serviceName='windows_exporter' function checkUninstall($serviceName){ $cpuArchitecture32bitPointerSize=4 $path=if ([IntPtr]::Size -eq $cpuArchitecture32bitPointerSize) {…

PowerShell: Installing a Program from Its Zip Archive

# installProgramFromExeZipArchive.ps1 # Automated installation of the racadm program $computerlist=@' HyperV01 HyperV02 '@ $fileURL="https://dl.dell.com/FOLDER08543783M/1/DellEMC-iDRACTools-Web-WINX64-10.3.0.0-4945.exe" $expectedExecutable='racadm.exe'…