To start off, this script is not what you are thinking it is. This was just something I coded up to double check that ports were open on some servers/client systems that I manage.
I didn't really need to make this, but it was a lot of fun trying to get it to work. You learn more by finding a problem and then trying to code a solution.
Now, this little guy, when called, will listen on whatever port you designated. It will holed at the $listener.AcceptTCPClient() method until something makes a connection on that port, as it is awaiting traffic.
I left the code in the comments for the command to run on the remote system (or vice versa) to connect on the port you are listening. It should return something to the effect of this, if it was successful.
Your focus here should be the "Connected : True" statement, showing you that the connection was successful (true).
Feedback and questions always welcome. 🤝
I didn't really need to make this, but it was a lot of fun trying to get it to work. You learn more by finding a problem and then trying to code a solution.
Code
# to start listener Listener.start();
# to sop listener Listerner.stop();
function Listener {
$input = Read-Host -Prompt "What port do you want to listen on: "
#convert variable string to integer
$port = [int]$input
#create socket for TCP listener
$listener = [System.Net.Sockets.TCPListener]$port
$listener.Start();
#listen for traffic on $port and accept connections made
$listener.AcceptTCPclient()
$listener.stop();
}
# on remote host use
# (New-Object System.Net.Sockets.TcpClient).Connect('target', $port)Now, this little guy, when called, will listen on whatever port you designated. It will holed at the $listener.AcceptTCPClient() method until something makes a connection on that port, as it is awaiting traffic.
I left the code in the comments for the command to run on the remote system (or vice versa) to connect on the port you are listening. It should return something to the effect of this, if it was successful.
Code
Client : System.Net.Sockets.Socket
Available : 0
Connected : True
ExclusiveAddressUse : False
ReceiveBufferSize : 65536
SendBufferSize : 65536
ReceiveTimeout : 0
SendTimeout : 0
LingerState : System.Net.Sockets.LingerOption
NoDelay : FalseYour focus here should be the "Connected : True" statement, showing you that the connection was successful (true).
Feedback and questions always welcome. 🤝


![[Image: pia_banner_3.png]](https://www.privateinternetaccess.com/affiliates/banners/pia_banner_3.png)
