【问题标题】:telnetlib python read_all() not working(hangs)telnetlib python read_all() 不工作(挂起)
【发布时间】:2020-08-11 22:32:40
【问题描述】:

我正在尝试使用 telnetlib 从 cisco 路由器读取数据

import telnetlib
tn = telnetlib.Telnet(’10.106.218.50’, 17280)
cmd1=”enable”
cmd2=”show run”
#session.write("command".encode('ascii') + b"\r")
tn.write(cmd1.encode('ascii') + b"\r")
tn.write(cmd2.encode('ascii') + b"\r")
#op=tn.read_very_eager()
#op=tn.read_some()
#op=tn.read_until('#')
op=tn.read_all()
print op

我能够成功写入路由器的控制台 但是,当我尝试从路由器的控制台读取数据时,系统就会挂起。 当我使用 read_some() 时,我得到了输出的一部分。但是 read_all() 只是挂起并且没有响应 请提出解决方案

【问题讨论】:

    标签: python-2.7 telnetlib cisco-ios


    【解决方案1】:

    read_all()
    

    如果在建立连接时没有指定超时,python 的 telnetlib 模块中的命令将阻塞。

    你的调用命令应该看起来像

    tn = telnetlib.Telnet('10.106.218.50', 17280, timeout = 1)
    

    您也可以替换自己的超时值。

    【讨论】:

      【解决方案2】:

      我的解决方案是使用 tn.red_until()。这段代码对我有用。 如果您不需要将输出发送到文本文件,则修改:

      data = data + tn.read_until(b"FIN\n", timeout = TIMEOUT).decode('ascii')
      

      作者:

      tn.read_until(b"FIN\n", timeout = TIMEOUT)
      

      这是我的代码:

      import sys
      import telnetlib
      import time
      
      username = "admin"
      password = "admin"
      command = "show version"
      TIMEOUT = 3
      
      router_list = open("hostlist.txt")
      for line in router_list:
          tn = telnetlib.Telnet(line.rstrip())
          tn.set_debuglevel(1)
          time.sleep(2)
          data = data + tn.read_until(b"Username: ").decode('ascii')
          tn.write(username.encode('ascii') + b"\n")
          time.sleep(2)
          data = data + tn.read_until(b"Password: ").decode('ascii')
          tn.write(password.encode('ascii') + b"\n")
          time.sleep(2)
          tn.write(command.encode('ascii') + b"\n")
          time.sleep(2)
          tn.write(b"\n")
          time.sleep(2)
          tn.write(b"echo FIN\n")
          time.sleep(2)
          data = data + tn.read_until(b"FIN\n", timeout = TIMEOUT).decode('ascii')
          print("Imprimiendo:" + data)
          op=open ("output.txt", "a").write(data)
          tn.close()
      

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题。

        import socket, telnetlib
        
        def telnet(ip_address,user_name,password):
        
            tn = telnetlib.Telnet(ip_address,23,1)
            # tn.set_debuglevel(2)
        
            tn.write(user_name + '\n')
            tn.write(password + '\n')
            tn.write('show version\n')
            tn.write('show power\n')
        
            print tn.read_all()
            tn.close()
        
        telnet('10.236.0.19','who','who')
        

        这里的错误日志是一样的:

        Traceback (most recent call last):
          File "telnet.py", line 41, in <module>
            telnet('10.236.0.19','who','who')
          File "telnet_tn.py", line 23, in telnet
            print tn.read_all()
          File "C:\Python27\lib\telnetlib.py", line 385, in read_all
            self.fill_rawq()
          File "C:\Python27\lib\telnetlib.py", line 576, in fill_rawq
            buf = self.sock.recv(50)
        socket.timeout: timed out
        

        然后我尝试修改C:\Python27\lib\telnetlib.py中的函数read_all()。之后,它按我的预期工作。你可以试试..

        之前:

        def read_all(self):
            """Read all data until EOF; block until connection closed."""
            self.process_rawq()
            while not self.eof:
                self.fill_rawq()
                self.process_rawq()
            buf = self.cookedq
            self.cookedq = ''
            return buf
        

        之后(为socket.timeout添加异常):

        def read_all(self):
            """Read all data until EOF; block until connection closed."""
            self.process_rawq()
            while not self.eof:
                try:
                    self.fill_rawq()
                    self.process_rawq()
                except:
                    break
            buf = self.cookedq
            self.cookedq = ''
            return buf
        

        【讨论】:

          猜你喜欢
          • 2016-07-27
          • 2018-04-15
          • 2019-01-09
          • 2017-01-23
          • 2021-02-24
          • 1970-01-01
          • 2014-10-11
          • 2012-05-15
          • 1970-01-01
          相关资源
          最近更新 更多