We have all worked on locked down hosts where the telnet client application is not installed and in the middle of a troubleshooting session comes the need to test network connectivity. But without the telnet client installed, it becomes challenging trying to validate network connectivity. We can utilize native tools and basic concepts to test this connectivity.
Below is how:
$ bash -c 'cat < /dev/null > /dev/tcp/www.google.com/80' $ echo $? 0
$? is a special shell variable that holds the exit status for the most recent foreground pipeline.
‘>’ is the redirect via the raw device /dev/<protocol>/<host>/<port>
An exit status of ‘0’ indicates success and any other value than ‘0’ will be an indication of failure to establish a connection. When the TCP socket is unavailable, it will take a while until the OS-defined timeout for the initiated connection to give up and most likely you will end up forcing an exit with Ctrl+C which also will yield a non-zero exit status.
Another one-liner can be used that will result in a ‘Port Open’ response only if the connection is successful.
$ bash -c 'cat < /dev/null > /dev/tcp/www.google.com/80' && echo "Port Open" Port Open $
The next time you are stuck trying to figure out if a TCP port is open, and are without a telnet client, use these basics to validate connectivity.
curl -v telnet://hostname:portnumber
curl is available on most Unix/Linux based dists. You can change the protocol to any of the following,
supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE).
Quick and handy ….