【问题标题】:Python telnet not showing resultPython telnet 不显示结果
【发布时间】:2021-08-07 02:52:57
【问题描述】:

使用 python 脚本我想在 mikrotik 路由器上进行一些配置,看起来脚本是正确的并且没有给出错误但没有打印命令输出而结束

import telnetlib
import time
dev_ip = "172.16.62.160"
user = "admin"
PASSWORD = ""
comm1 = "ip address print"

tn = telnetlib.Telnet(dev_ip, timeout=1)
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.read_until(b">")
time.sleep(1)
tn.write(comm1.encode("ascii") + b"\r\n")
Showcmdoutput = tn.read_very_eager().decode('ascii')
print(Showcmdoutput)
tn.close()
print("DONE")

在 Ubuntu 桌面上运行

【问题讨论】:

  • 如果在路由器开始响应您发送的命令之前有一点延迟,.read_very_eager() 将返回一个空字符串 - 它不会等待数据可用。命令后的简短sleep() 可能会有所帮助,或者如果有一些保证字符串将始终出现在命令结果的末尾,则更改为使用.read_until()

标签: python telnetlib


【解决方案1】:

放置后问题解决:

time.sleep(1)

Showcmdoutput = tn.read_very_eager().decode('ascii')之前

tn.write(comm1.encode("ascii") + b"\r\n")
time.sleep(1)
Showcmdoutput = tn.read_very_eager().decode('ascii')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 2020-12-22
    • 2012-11-03
    • 2014-07-25
    • 1970-01-01
    • 2016-09-17
    • 2015-05-13
    相关资源
    最近更新 更多