介绍
实例
登录设备执行命令
- 代码
from telnetlib import Telnet ######################################## 设置变量 ######################################## host = '192.168.85.202' port = 23 username = 'admin' password = 'admin' ######################################## 设置变量 ######################################## tn = Telnet(host=host,port=port,timeout=5) # 如果定义了变量username,则输入用户名 if "username" in vars(): # 读取回显,判断出现"Username:"字符 tn.read_until(b'Username:',timeout=1) # 发送用户名和回编码为字节 tn.write(username.encode("utf-8") + b'\n') # 读取回显,判断出现字符"Password:" tn.read_until(b'Password:') # 发送密码和回车 tn.write(password.encode("utf-8") + b'\n') # 发送命令 tn.read_until(b'>',timeout