Saturday, July 15, 2017

Testing if a remote port is open using BASH /dev/tcp/host/port

In a previous post, Opening an inbound port for an Ubuntu Virtual Machine on Azure, an inbound port was open on an Ubuntu virtual machine (port 21777) and on this host a service (tinyproxy) was installed that listens on this port via TCP/IP. Obviously, there should be a way to test that the port is actually open and if the underling service is actually listening. This can be accomplished by BASH where a host/ip and port can be accessed using either of the following:

/dev/tcp/host/port
/dev/udp/host/port

The version to use depends on the type of connection to be opened to the port, tcp or udp. SSH (port 22) was used to access the Ubuntu virtual machine (IP address 52.183.120.51). From a remote machine's BASH shell (e.g. a Macbook Pro's Terminal) the following can be entered:

cat < /dev/tcp/52.183.120.51/22

As we can see from the screen shot port 22 is open on the host because there was a response ("SSH-2.0-OpenSSH_7.4p1 Ubuntu-10") when port 22 was opened:

Not every protocol is so obvious when connected to via TCP. Connecting to port 21777 (which is open for the host):

The connection above was success because it does not time out. Clearly the proxy is connected but does not send any kind of a reply on the initial TCP connection.

Connecting to a port that is not open such as 21778 results in the following (a time out):


The time out is not returned immediately but it does indicate the port is not open or that no service is listening to 21778 on TCP.




No comments :

Post a Comment