telnetlib 是 Python 标准库中的一个模块,专用于编写 Telnet 协议的客户端程序。Telnet(TELecommunication NETwork)是早期的远程登录协议,通过基于文本的方式进行命令交互,主要用于远程登录服务器或设备进行管理。
常见应用场景:
(1)自动化远程管理 Telnet 设备(如路由器、交换机等)。
(2)测试支持 Telnet 的网络服务。
(3)编写交互式脚本批量发送命令。
(4)与旧版系统集成远程终端会话。
◆ ◆ ◆
核心概念
1、Telnet 协议
工作在 TCP 23 端口,基于明文的远程控制协议。
telnetlib 的核心类是 Telnet,其本质是对 TCP Socket 的封装,专门针对 Telnet 协议交互进行了增强。
2、Telnet 会话
连接建立后,用户通过发送命令并接收响应与远程主机交互。
3、延时与阻塞
Telnet 会话常涉及等待响应或指定字符串出现。
4、匹配读取
expect() 方法支持使用正则表达式读取直到特定内容。
◆ ◆ ◆
应用举例
例 1:连接 Telnet 服务器并登录
import telnetlib
host = "your.telnet.server"
user = "admin"
password = "123456"
tn = telnetlib.Telnet(host)
tn.read_until(b"login: ")
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"ls\n") # 发送命令
tn.write(b"exit\n")
print(tn.read_all().decode('utf-8'))例 2:读取特定响应并处理超时
import telnetlib
tn = telnetlib.Telnet("localhost", 8023, timeout=5)
tn.write(b"help\n")
index, match, data = tn.expect([b"exit", b"help"], timeout=3)
print("Matched pattern index:", index)
print("Output:", data.decode())例 3:启用调试模式以查看发送/接收内容
import telnetlib
tn = telnetlib.Telnet("localhost")
tn.set_debuglevel(1) # 显示调试信息
tn.write(b"hello\n")
tn.close()例 4:从 Telnet 中读取多行输出
import telnetlib
tn = telnetlib.Telnet("localhost")
tn.write(b"show config\n")
output = tn.read_until(b"#", timeout=5)
print(output.decode())
tn.close()例 5:异常处理与断开连接
import telnetlib
try:
tn = telnetlib.Telnet("192.168.1.1", timeout=3)
tn.write(b"status\n")
print(tn.read_all().decode())
except Exception as e:
print("连接失败:", e)
finally:
tn.close()◆ ◆ ◆
常用函数速览
close()
关闭 Telnet 连接。
参数:无
返回:无
expect(list_of_regex, timeout=None)
等待直到输入匹配 list_of_regex 中的任一项。
参数:
list_of_regex:一个正则表达式字节串列表
timeout:超时(秒)
返回:(匹配索引, 匹配对象, 匹配前的输出数据)
get_socket()
返回内部使用的原始 socket 对象(可用于设置超时等操作)。
参数:无
返回:socket.socket 对象
open(host, port=23, timeout=10)
连接远程 Telnet 主机。
参数:
host:主机名或 IP 地址
port:端口(默认 23)
timeout:连接超时(秒)
返回:无(如失败抛出异常)
read_all()
读取所有可用数据直到连接关闭。
参数:无
返回:读取的字节串
read_very_eager()
尽可能多地读取当前可用的数据。
参数:无
返回:字节串
read_until(expected, timeout=None)
读取直到遇到期望的字节串或超时。
参数:
expected:目标字节串
timeout:可选,秒数
返回:读取的字节串(包括 expected)
set_debuglevel(level)
设置调试输出级别(0=关闭,1=输出所有通信)。
参数:整数(调试等级)
返回:无
write(buffer)
向 Telnet 服务器发送数据。
参数:字节串(如需发送文本,需编码为 bytes)
返回:无
Telnet(host=None, port=23, timeout=socket._GLOBAL_DEFAULT_TIMEOUT)
创建一个 Telnet 客户端对象,并可选地立即连接到指定主机与端口。如果未提供 host 参数,可稍后调用 .open() 方法进行连接。
参数:
host(可选):字符串类型,远程主机名或 IP 地址。如未指定,则不会立即连接
port(可选):整数类型,远程主机的端口号,默认值为 23(标准 Telnet 端口)
timeout(可选):浮点数,单位为秒,表示连接超时时间。可使用 socket._GLOBAL_DEFAULT_TIMEOUT 表示使用全局默认值
返回:Telnet 实例对象,提供一系列方法进行 Telnet 通信(如 .read_until()、.write() 等)
示例:
import telnetlib
# 方式一:立即连接
tn1 = telnetlib.Telnet("192.168.1.100", 23, timeout=10)
# 方式二:延迟连接
tn2 = telnetlib.Telnet()
tn2.open("192.168.1.100", 23)◆ ◆ ◆
补充说明
1、telnetlib 不支持加密通信,慎用于生产环境。
2、在很多现代操作系统中,Telnet 默认是关闭的,可使用软件如 telnetd 启动服务。
3、可用 putty、BusyBox、OpenWRT 等环境测试 Telnet。
4、Telnet 适用于调试简易协议或模拟控制台行为。
“点赞有美意,赞赏是鼓励”
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.