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 |
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
bash <(echo 'exec 3<>/dev/tcp/IP/PORT; cat <&3 & echo "$$ $!"; line="Type and hit enter to send a message to the client, enter no message to terminate"; while : ; do echo "$line" >&3; if [ -z "$line" ]; then break; fi; read line; done; kill $!') | bash <(echo 'echo -e "\e[01;36mConnecting\e[0m"; read pid1 pid2; echo -e "\e[01;36mConnected!\e[0m"; while read line; do if [ -z "$line" ]; then kill $pid1 $pid2; fi; echo -e "\e[01;32m$line\e[0m"; done')
emulates 3_client very closely
awk 'BEGIN { print "Listening"; start=1; line="-"; while (length(line)) { ARGV[1] |& getline line; if (start) { start=0; print "Connected!"; print line; print "Enter - to await next message" } else print "\x1B[A"line; while (length(line)) { getline line <"-"; if (line == "-") break; print line |& ARGV[1] } } }' '/inet/tcp/PORT/0/0'
is an okay server
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1_*.ps1
is a simple client->server communication,2_*.ps1
is a glitchy client<->server communication, and3_*.ps1
is realtime client<->server communication that's basically a prettier linuxnc IP PORT
<->nc -l PORT