Standard Telnetlib Redistribution
standard-telnetlib is a redistribution of the `telnetlib` module, which was part of the Python standard library until its removal in Python 3.13 (deprecated in 3.11). It serves as a 'dead battery' backport, providing the original `telnetlib` functionality for projects that require it in Python versions where it's no longer built-in. This library allows developers to continue using the `telnetlib` client functionality without needing to pin older Python versions. The latest version is 3.13.0, released to coincide with Python 3.13's removal of the module.
Common errors
-
ModuleNotFoundError: No module named 'telnetlib'
cause The `telnetlib` module was removed from the Python standard library in Python 3.13.fixInstall the `standard-telnetlib` package: `pip install standard-telnetlib`. -
TypeError: a bytes-like object is required, not 'str'
cause Attempting to write a Python string object directly to the Telnet connection, which expects bytes.fixEncode the string to bytes before writing: `tn.write('my_command'.encode('ascii') + b'\n')`. -
socket.gaierror: [Errno 10109] getaddrinfo failed
cause This error can occur if the `port` argument passed to `telnetlib.Telnet()` is a string instead of an integer, or if the `host` is unresolvable.fixEnsure the `port` is an integer: `telnetlib.Telnet(HOST, int(PORT))`. Also, verify the `HOST` value is a correct IP address or resolvable hostname. -
EOFError: Telnet connection closed
cause The remote Telnet server closed the connection unexpectedly while the client was attempting to read data, or `read_until` could not find the expected string within the timeout.fixCheck the remote Telnet server's status and logs. Increase the `timeout` parameter in `read_until()` calls. Ensure that the expected string passed to `read_until()` accurately matches the server's output, including case and trailing spaces/newlines.
Warnings
- breaking The `telnetlib` module was removed from the Python standard library in Python 3.13 (after being deprecated in 3.11). While `standard-telnetlib` provides a backport, direct reliance on `telnetlib` without this redistribution will cause `ModuleNotFoundError` on Python 3.13 and newer.
- breaking The Telnet protocol transmits all data, including usernames and passwords, in cleartext. This makes it highly vulnerable to eavesdropping and session hijacking, and it is not suitable for secure communication over untrusted networks.
- gotcha All data written to the Telnet connection must be `bytes`, and all data read from the connection will be `bytes`. Incorrectly sending `str` or failing to decode received `bytes` will lead to `TypeError` or unexpected output.
- gotcha The `port` argument to `telnetlib.Telnet()` constructor must be an integer. Passing a string will result in a `socket.gaierror` (e.g., `[Errno 10109] getaddrinfo failed`) or other connection errors.
Install
-
pip install standard-telnetlib
Imports
- Telnet
from standard_telnetlib import Telnet
from telnetlib import Telnet
Quickstart
import telnetlib
import getpass
import os
HOST = os.environ.get('TELNET_HOST', 'localhost')
PORT = int(os.environ.get('TELNET_PORT', 23))
USER = os.environ.get('TELNET_USER', 'testuser')
try:
# Establish connection
tn = telnetlib.Telnet(HOST, PORT, timeout=5)
# Read until a login prompt, send username
tn.read_until(b'login: ', timeout=2)
tn.write(USER.encode('ascii') + b'\n')
# Read until password prompt, send password
if os.environ.get('TELNET_PASSWORD'): # Only prompt if env var is set
PASSWORD = getpass.getpass()
tn.read_until(b'Password: ', timeout=2)
tn.write(PASSWORD.encode('ascii') + b'\n')
# Example: Send a command and read output
tn.write(b'ls -l\n')
output = tn.read_until(b'$ ', timeout=5) # Assuming a common shell prompt
print(output.decode('ascii'))
tn.write(b'exit\n')
print(tn.read_all().decode('ascii'))
except Exception as e:
print(f"An error occurred: {e}")
finally:
if 'tn' in locals() and tn:
tn.close()