Telnetting without the telnet command

Do you need to test connectivity between a server or container and another host/port, but the telnet command (or any other equivalent tool) is missing?

Don’t despair, my young mafagafo. If your server or container has the Bash shell, you can still test connectivity to any host and port.

Just use this “secret” Bash feature:

timeout 1 bash -c 'cat < /dev/tcp/google.com/80' && echo "Port open" || echo "Port closed"

Note

google.com and port 80 are just examples. You should replace them with the specific host and port you want to test from your source server or container.

But why does this work?

When Bash encounters a redirection pointing to a path starting with /dev/tcp/HOST/PORT, it intercepts the call before passing it to the operating system. Instead of trying to open a physical file, Bash performs the following steps:

Parses the Path: It extracts the Hostname/IP and the Port from the string.

Establishes Connection: It uses standard C library network functions (like socket() and connect()) to establish a real TCP connection.

Creates a File Descriptor: It creates a file descriptor that the shell can then use to read or write data through that connection.

Important

If the command fails despite having the correct syntax, it is probably because Bash was compiled with the --disable-net-redirections option, so unfortunately it will not be possible to test

Warning

This is only possible to do in Bash, so if you try to do it in another shell it won’t work.

And that’s it folks, today we learned that a good shell can make your life easier.