# TEst-TCPTimeout # # Simple TCP-server to listen on a given port and send a Timestamp and Remote IP back with ascending delays # Used to check Firewall, NAT Timeouts, Port reachability, externe IP-Address # # https://www.msxfaq.de/netzwerk/grundlagen/tcp_session_timeout.htm param ( $tcpport=25, $delay = (1,5,31,62,122,662,3600,7200,14400) ) write-host "Start new Listner on port $($tcpport) " $endpoint = new-object System.Net.IPEndPoint([system.net.ipaddress]::any, $tcpport) $listener = new-object System.Net.Sockets.TcpListener $endpoint $listener.start() # warte auf eingehende Verbindungen # halte sie, bis sie von einem Accept abgeholt werden. while (!$endscript) { write-host "---- Wait for connections on Port $($tcpport)(Blocking)" $connection = $listener.AcceptTcpClient() # Blockiert das Skript bis eine Verbindung ankommt $remoteip = "$($connection.Client.RemoteEndPoint.Address.IPAddressToString):$($connection.Client.RemoteEndPoint.Port.IPAddressToString)" write-host "New Connection from RemoteIP: $($remoteip)" $stream = $connection.GetStream() # Binde Datenstrom [bool]$endscript = $false foreach ($step in $delay) { [string]$message = "Test-TCPConnection $($remoteip) $((get-date).ToString(""o"")) Wait $($step) seconds`n`r" Write-Host "Sending: $($message)" -NoNewline $data = [text.Encoding]::Ascii.GetBytes($message) try { $stream.Write($data,0,$data.length) $stream.Flush() } catch { write-host "Problem sending message $($_.exception.message)" -ForegroundColor red break } for ($i = 0; $i -lt $step; $i++) { start-sleep -seconds 1 Write-Progress -id 1 -Activity "Wait for Timeout" -SecondsRemaining ($step-$i) -PercentComplete ($i/$step*100) if ([console]::KeyAvailable) { $keycode = [System.Console]::ReadKey($true) if ($keycode.key -eq "X") { Write-host "X Detected" $endscript = $true break # leave current loop } else { Write-host "Press X to stop" } } } Write-Progress -id 1 -Completed -Activity "Wait for Timeout" if ($endscript) { break # leave current loop } } Write-host "Closing Connection" $stream.close() $connection.Close() # Verbindung beenden } $listener.stop() write-host "Test-TCPTimeout - Done"