Last active
December 17, 2023 11:36
-
-
Save Hashbrown777/95a79a3f9fc86090a38375a3314c1043 to your computer and use it in GitHub Desktop.
Listen and send basic messages over TCP to see if your ports are open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
&{ Param($hostname, $port) | |
$socket = $NULL | |
$stream = $NULL | |
try { | |
$socket = [System.Net.Sockets.TCPClient]::new( | |
[System.Net.IPAddress]::Parse( | |
[System.Net.Dns]::GetHostAddresses($hostname) | |
), | |
$port | |
) | |
$stream = $socket.GetStream() | |
$writer = [System.IO.StreamWriter]::new($stream) | |
'Connected! Send something to the server' | |
'Hit enter with no text to exit' | |
for ($line = $NULL; $line = Read-Host; $writer.Flush()) { | |
$writer.WriteLine($line) | |
} | |
} | |
finally { | |
$stream.Close() | |
$socket.Close() | |
} | |
} -hostname (iwr -Uri 'https://ifconfig.me/ip').Content -port 17011 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
&{ Param($ip, $port) | |
$listener = [System.Net.Sockets.TcpListener]::new( | |
[System.Net.IPEndPoint]::new($ip, $port) | |
) | |
$listener.Start() | |
$client = $NULL | |
$stream = $NULL | |
$reader = $NULL | |
try { | |
$client = $listener.AcceptTcpClient() | |
$stream = $client.GetStream() | |
$reader = [System.IO.StreamReader]::new($stream) | |
'Connected!' | |
'The client will send text and close this session' | |
for ( | |
$line = $NULL; | |
$line = $reader.ReadLine(); | |
Write-Host $line -fore cyan | |
) {} | |
} | |
finally { | |
$reader.Dispose() | |
$stream.Dispose() | |
$client.Dispose() | |
} | |
$listener.Stop() | |
} -ip ([System.Net.IPAddress]::Any) -port 17011 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
&{ Param($hostname, $port) | |
Write-Host 'Connecting, ctrl+c to exit' -fore cyan | |
$socket = $NULL | |
$stream = $NULL | |
$writer = $NULL | |
$response = $NULL | |
$chat = $NULL | |
try { | |
$socket = [System.Net.Sockets.TCPClient]::new( | |
[System.Net.IPAddress]::Parse( | |
[System.Net.Dns]::GetHostAddresses($hostname) | |
), | |
$port | |
) | |
$stream = $socket.GetStream() | |
$writer = [System.IO.StreamWriter]::new($stream) | |
$response = [System.IO.StreamReader]::new($stream) | |
Write-Host 'Connected!' -fore cyan | |
$line = 'Type and hit enter to send a message to the client, requests are then shown' | |
while ($socket.Connected) { | |
if ($line) { | |
$writer.WriteLine($line) | |
$writer.Flush() | |
Sleep -Milliseconds 100 | |
} | |
while ($stream.DataAvailable) { | |
$line = $response.ReadLine() | |
if (!$line) { | |
exit | |
} | |
Write-Host $line -fore green | |
} | |
$line = Read-Host | |
} | |
} | |
finally { | |
try { | |
$writer.WriteLine('') | |
$writer.Flush() | |
} | |
catch {} | |
$response.Close() | |
$writer.Close() | |
$socket.Close() | |
} | |
} -hostname (iwr -Uri 'https://ifconfig.me/ip').Content -port 30120 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
&{ Param($ip, $port) | |
$listener = [System.Net.Sockets.TcpListener]::new( | |
[System.Net.IPEndPoint]::new($ip, $port) | |
) | |
$listener.Start() | |
Write-Host 'Listening, ctrl+c to exit' -fore cyan | |
$client = $NULL | |
$stream = $NULL | |
$reader = $NULL | |
$response = $NULL | |
$chat = $NULL | |
try { | |
$client = $listener.AcceptTcpClient() | |
$stream = $client.GetStream() | |
$response = [System.IO.StreamWriter]::new($stream) | |
$reader = [System.IO.StreamReader]::new($stream) | |
Write-Host 'Connected!' -fore cyan | |
$line = 'Type and hit enter to send a message to the server, responses are then shown' | |
while ($client.Connected) { | |
if ($line) { | |
$response.WriteLine($line) | |
$response.Flush() | |
Sleep -Milliseconds 100 | |
} | |
while ($stream.DataAvailable) { | |
$line = $reader.ReadLine() | |
if (!$line) { | |
exit | |
} | |
Write-Host $line -fore green | |
} | |
$line = Read-Host | |
} | |
} | |
finally { | |
try { | |
$response.WriteLine('') | |
$response.Flush() | |
} | |
catch {} | |
$reader.Close() | |
$response.Close() | |
$client.Close() | |
$listener.Stop() | |
} | |
} -ip ([System.Net.IPAddress]::Any) -port 30120 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#NetStream doesn't support ReadStream & WriteStream operating at the same time | |
#so instead of writing directly here we have to use a queue later | |
$queue = [System.Collections.Queue]@('Type and hit enter to send a message to the client, enter no message to terminate') | |
$chat = $NULL | |
if ($MyInvocation.ExpectingInput) { | |
$chat = [PSCustomObject]@{Input=$Input} ` | |
| Add-Member ` | |
-MemberType ScriptProperty ` | |
-Name 'HasExited' ` | |
-Value { | |
if (!$this.Input) { | |
return $True | |
} | |
if ($this.Input.MoveNext()) { | |
$queue.Enqueue($this.Input.Current) | |
} | |
else { | |
$queue.Enqueue('') | |
$this.Kill() | |
} | |
return $False | |
} ` | |
-PassThru ` | |
| Add-Member ` | |
-MemberType ScriptMethod ` | |
-Name 'Kill' ` | |
-Value { $this.Input = $NULL } ` | |
-PassThru | |
} | |
&{ Param($hostname, $port) | |
Write-Host 'Connecting' -fore cyan | |
$socket = $NULL | |
$stream = $NULL | |
$writer = $NULL | |
$response = $NULL | |
try { | |
$socket = [System.Net.Sockets.TCPClient]::new( | |
[System.Net.IPAddress]::Parse( | |
[System.Net.Dns]::GetHostAddresses($hostname) | |
), | |
$port | |
) | |
$stream = $socket.GetStream() | |
$writer = [System.IO.StreamWriter]::new($stream) | |
$writer.NewLine = "`n" | |
$response = [System.IO.StreamReader]::new($stream) | |
Write-Host 'Connected!' -fore cyan | |
if (!$chat) { | |
$chat = [Diagnostics.Process]::new() | |
$chat.StartInfo ` | |
| %{ | |
$_.UseShellExecute = $False | |
$_.RedirectStandardOutput = $True | |
$_.FileName = 'powershell' | |
$_.Arguments = '-Command "&{$line=$NULL;do{$line=Read-Host;$line}while($line);1|sleep}"' | |
} | |
Register-ObjectEvent ` | |
-InputObject $chat ` | |
-EventName 'OutputDataReceived' ` | |
-MessageData $queue ` | |
-Action { | |
$Event.MessageData.Enqueue($EventArgs.Data) | |
} ` | |
| Out-Null | |
$chat.Start() | Out-Null | |
$chat.BeginOutputReadLine() | |
} | |
$line = $NULL | |
do { | |
while ($queue.Count) { | |
$line = $queue.Dequeue() | |
$writer.WriteLine($line) | |
$writer.Flush() | |
} | |
while ($stream.DataAvailable) { | |
$line = $response.ReadLine() | |
if ($line) { | |
Write-Host $line -fore green | |
} | |
else { | |
$chat.Kill() | |
} | |
} | |
sleep -Milliseconds 500 | |
} while (!$chat.HasExited) | |
} | |
finally { | |
if (!$chat.HasExited) { | |
$chat.Kill() | |
} | |
$response.Close() | |
$writer.Close() | |
$socket.Close() | |
} | |
} -hostname (iwr -Uri 'https://ifconfig.me/ip').Content -port 30120 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#NetStream doesn't support ReadStream & WriteStream operating at the same time | |
#so instead of writing directly here we have to use a queue later | |
$queue = [System.Collections.Queue]@('Type and hit enter to send a message to the server, enter no message to disconnect') | |
$chat = $NULL | |
if ($MyInvocation.ExpectingInput) { | |
$chat = [PSCustomObject]@{Input=$Input} ` | |
| Add-Member ` | |
-MemberType ScriptProperty ` | |
-Name 'HasExited' ` | |
-Value { | |
if (!$this.Input) { | |
return $True | |
} | |
if ($this.Input.MoveNext()) { | |
$queue.Enqueue($this.Input.Current) | |
} | |
else { | |
$queue.Enqueue('') | |
$this.Kill() | |
} | |
return $False | |
} ` | |
-PassThru ` | |
| Add-Member ` | |
-MemberType ScriptMethod ` | |
-Name 'Kill' ` | |
-Value { $this.Input = $NULL } ` | |
-PassThru | |
} | |
&{ Param($ip, $port) | |
$listener = [System.Net.Sockets.TcpListener]::new( | |
[System.Net.IPEndPoint]::new($ip, $port) | |
) | |
$listener.Start() | |
Write-Host 'Listening' -fore cyan | |
$client = $NULL | |
$stream = $NULL | |
$reader = $NULL | |
$response = $NULL | |
try { | |
$client = $listener.AcceptTcpClient() | |
$stream = $client.GetStream() | |
$response = [System.IO.StreamWriter]::new($stream) | |
$response.NewLine = "`n" | |
$reader = [System.IO.StreamReader]::new($stream) | |
Write-Host 'Connected!' -fore cyan | |
if (!$chat) { | |
$chat = [Diagnostics.Process]::new() | |
$chat.StartInfo ` | |
| %{ | |
$_.UseShellExecute = $False | |
$_.RedirectStandardOutput = $True | |
$_.FileName = 'powershell' | |
$_.Arguments = '-Command "&{$line=$NULL;do{$line=Read-Host;$line}while($line);1|sleep}"' | |
} | |
Register-ObjectEvent ` | |
-InputObject $chat ` | |
-EventName 'OutputDataReceived' ` | |
-MessageData $queue ` | |
-Action { | |
$Event.MessageData.Enqueue($EventArgs.Data) | |
} ` | |
| Out-Null | |
$chat.Start() | Out-Null | |
$chat.BeginOutputReadLine() | |
} | |
$line = $NULL | |
do { | |
while ($queue.Count) { | |
$line = $queue.Dequeue() | |
$response.WriteLine($line) | |
$response.Flush() | |
} | |
while ($stream.DataAvailable) { | |
$line = $reader.ReadLine() | |
if ($line) { | |
Write-Host $line -fore green | |
} | |
else { | |
$chat.Kill() | |
} | |
} | |
sleep -Milliseconds 500 | |
} while (!$chat.HasExited) | |
} | |
finally { | |
if (!$chat.HasExited) { | |
$chat.Kill() | |
} | |
$reader.Close() | |
$response.Close() | |
$client.Close() | |
$listener.Stop() | |
} | |
} -ip ([System.Net.IPAddress]::Any) -port 30120 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bash <(echo 'exec 3<>/dev/tcp/IP/PORT; cat <&3 & while read line; do echo "$line" >&3; done')
is a good client alternative for linux if you dont have
nc
emulates 3_client very closely
is an okay server