Test if a port on a remote system is reachable (without telnet)
In the old days, we used telnet
to see if a port on a remote host was open: telnet hostname port
would attempt to connect to any port on any host and give you access to the raw TCP stream.
These days, the systems I work on do not have telnet installed (for security reasons), and all outbound connections to all hosts are blocked by default. Over time, it's easy to lose track of which ports are open to which hosts.
Is there another way to test if a port on a remote system is open – using a Linux system with a limited number of packages installed, and telnet
is not available?
Solution 1:
Bash has been able to access TCP and UDP ports for a while. From the man page:
/dev/tcp/host/port
If host is a valid hostname or Internet address, and port is an integer port number
or service name, bash attempts to open a TCP connection to the corresponding socket.
/dev/udp/host/port
If host is a valid hostname or Internet address, and port is an integer port number
or service name, bash attempts to open a UDP connection to the corresponding socket.
So you could use something like this:
xenon-lornix:~> cat < /dev/tcp/127.0.0.1/22
SSH-2.0-OpenSSH_6.2p2 Debian-6
^C pressed here
Taa Daa!
Solution 2:
Nice and verbose! From the man pages.
Single port:
nc -zv 127.0.0.1 80
Multiple ports:
nc -zv 127.0.0.1 22 80 8080
Range of ports:
nc -zv 127.0.0.1 20-30
Solution 3:
Netcat is a useful tool:
nc 127.0.0.1 123 &> /dev/null; echo $?
Will output 0
if port 123 is open, and 1
if it's closed.