PowerShell : Telnet TCP\UDP ports on Multiple Machines


I believe testing the ports after a Firewall maintenance or in another network related scenario could be real tiresome. Well, PowerShell enables us to automate this stuff as well.

Now no more running the cmd prompt and firing the telnet queries from your local or a remote machine to some destination computers:)

ezgif.com-video-to-gif (2)HOW IT WORKS :

We will use two classes of System.Net.Sockets namespace, to query the ports like the way we test them using the Telnet utility.

  1. System.Net.Sockets.TCPClient : Class to create an object to query  the status of TCP ports
  2. System.Net.Sockets.UDPClient : Class to create an object to query  status the of UDP ports

 

To understand these classes better, you can refer the Microsoft article Here , which describes the properties and methods that could be utilized as per our requirement by the instances of these classes.

Tesing TCP ports : I would say TCP part is pretty easy and reliable, which is the reason of the popularity of TCP (Transmission Control Protocol) which establishes a connection and sends an ACK (Acknowledgement). which makes it reliable and straightforward to use, like below

tcp

Testing UDP ports : UDP ports are tricky to test and results aren’t reliable as UDP protocol doesn’t send ACK’s (Connectionless protocol) so testing them is difficult. As there is no guarantee that you’ll receive a response.

To workaround this we’ll use System.Net.Sockets.UDPClient‘s  Send() method to send Datagrams to destination IPAdress:Port and expect (Wait) for a response from the destination.

UDP

NOTE :  Please note in most of the cases you won’t receive a response, as UDP protocol is a connectionless protocol, but when you’ll run the UDP port test in a loop, you will see some responses getting captured from the destination confirming that port is open. Like in below animation.

ezgif.com-cropGET THE SCRIPT : Click here to go to my GitHub gist to get the code.

HOW TO USE IT :

Feed in Source, Destination and Port Number to the function and run it to get the status of Ports.

The script prompt for your Administrator Credentials if you give source other than you local machine, So that it can log onto remote Source and  invoke Telnet-Port function to test destination ports.

reds

Also, Protocol  is set to  ‘TCP‘  By default, like in the animation below.

ezgif.com-gif-maker (1)

If you want  UDP  Protocol use the switch -Protocol followed by UDP to test the UDP ports, like below.UDPMoreover, there is an -Iterate switch through which you can Continuous Telnet, just like we do continuous ping’s (ping -t 127.0.0.1)

iterateThis function made my life a bit easy in some network related testing, Hope helps you in the same way.

Thank you for Stopping by 🙂 Happy Reading folks.

Advertisements

One thought on “PowerShell : Telnet TCP\UDP ports on Multiple Machines

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s