通过cmd

时间:2018-04-30 08:34:36

标签: python python-2.7 telnet telnetlib

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

enter image description here

如您所见,它需要:

  1. " user>" (回答" su")然后
  2. "密码>" (回答" 1234")然后提示
  3. "#mp4100"
  4. 撰写"?" (即使没有输入)提示会吐出一个巨大的字符串。 我试图模仿这个,但似乎没有任何工作......

    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帮助中看到它)

    Telnet(172.17.174.241,23): recv '\xff\xfb\x01'
    Telnet(172.17.174.241,23): IAC WILL 1
    Telnet(172.17.174.241,23): recv '\xff\xfb\x03'
    Telnet(172.17.174.241,23): IAC WILL 3
    Telnet(172.17.174.241,23): recv '\xff\xfc\x01'
    Telnet(172.17.174.241,23): IAC WONT 1
    Telnet(172.17.174.241,23): recv '\xff\xfc\x03'
    Telnet(172.17.174.241,23): IAC WONT 3
    Telnet(172.17.174.241,23): recv '\n\r'
    Telnet(172.17.174.241,23): recv '\n\ruser>'
    Traceback (most recent call last):
    

0 个答案:

没有答案