telnet连接在使用cmd时超时

2024-10-03 06:32:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Telnet连接到我的远程设备。通过cmd使用telnet时,我没有遇到任何问题,提示如下所示

enter image description here

如您所见,它需要:

  1. “用户”(回答“su”),然后
  2. “password>;”(回答“1234”),然后提示是
  3. “mp4100”

写“?”(即使没有输入)提示符会发出一个巨大的字符串。 我试着模仿这个,但似乎没有任何效果。。。在

import telnetlib

HOST = "172.17.174.241"
user = 'su'
password = '1234'
time_out = 2
name = "mp4100"
prompt = name + '#'

tn = telnetlib.Telnet(HOST, timeout = time_out)
tn.set_debuglevel(100)

tn.read_until("user>", time_out)
tn.write(user + "\n")

if password:
    tn.read_until("password>", time_out)
    tn.write(password + "\n")

#tn.read_until(prompt, time_out)
tn.write('?')

print tn.read_all() # shouldn't this write the string that the prompt wrote?
tn.close()

当我运行这个时,它就冻结了。在


编辑:我给连接添加了超时,这就是它冻结的地方。设备不会通过脚本连接,即使通过cmd它也没有问题


代码编辑了一点以显示我现在在做什么。添加tn.set_debuglevel(100)后的输出(此函数的合法值是多少?在doc/spyder帮助中没有看到)

^{pr2}$

Tags: cmdhostreadtimepasswordoutprompttelnet