嵌入式设备日志通过python实现telnet登录后过滤输出
from telnetlib import Telnet
def get_filtered_device_info(host, username, password):
try:
tn = Telnet(host)
# 登录
output = tn.read_until(b"stb login: ",timeout=3)
tn.write(username.encode('ascii') + b"\n")
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
# 等待提示符
tn.read_until(b"#")
# 开启打印信息
tn.write(b"log on\n")
tn.read_until(b"#")
# 输出信息
try:
while True:
output = tn.read_until(b"\n").decode()
if output.find("过滤日志内容") > 0:
print(output,end='')
except KeyboardInterrupt:
print("程序终止")
finally:
tn.close()
except Exception as e:
print(f"Error: {str(e)}")
return []
finally:
tn.close()
# 使用时修改IP,登录密码
if __name__ == "__main__":
print("程序已启动,按[Ctrl+C]退出")
get_filtered_device_info("192.168.1.201", "root", "")

浙公网安备 33010602011771号