When There’s No Telnet or Netcat

Sometimes I need to test an Internet connection from a machine where neither telnet nor netcat is installed. Python, which is practically always preinstalled on Linux servers, provides a simple solution.

# python
>>> import socket
>>> s = socket.socket()
>>> s.connect((host, port))

If the connect() call returns immediately, then the connection (including DNS lookup) was successful.