搜尋

首頁  >  問答  >  主體

类库 - python模块telnetlib的buffer size如何修改

使用Python的第三方模块telnetlib,来对交换机执行批量操作,执行返回值数据量大的命令是出现异常,看报错应该是buffer的size被限制了,不知道如何修改,不知道大家有没有做过的?
代码和异常在下方。

代码:

#!usr/bin/env python
# coding:utf-8

import threading
import telnetlib
import getpass
import socket
import time

class TelnetThreadClass(threading.Thread):

    def __init__(self, username, password, command, host, port=23, timeout=5):
        threading.Thread.__init__(self)
        self.host = host
        self.port = port
        self.timeout = timeout
        self.username = username
        self.password = password
        self.command = command

    def run(self):
        self.setName(self.host)
        session = telnetlib.Telnet(self.host, self.port, self.timeout)
        session.read_until("Username: ")
        session.write(self.username + '\n')
        if self.password:
            session.read_until("Password: ")
            session.write(self.password + '\n')
        # for command in self.commands:
            session.write("{0}\n".format(command))
        session.write("exit\n")
        print session.read_all()
        print '*********************************************'


if __name__ == '__main__':
    hosts = ['10.1.1.1', '10.1.1.2']
    command = 'show version'
    username = 'username'  # raw_input("Enter your username: ")
    password = 'password'   # getpass.getpass('Enter your password: ')
    for host in hosts:
        my_telnet = TelnetThreadClass(username, password, command, host)
        my_telnet.start()
        print my_telnet.name
        my_telnet.join()

异常:

Exception in thread 10.1.1.1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 801, in __bootstrap_inner
    self.run()
  File "E:/PycharmProjects/MyTest/demo/pex_demo/telnet_mananger.py", line 34, in run
    print session.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)
timeout: timed out
高洛峰高洛峰2769 天前912

全部回覆(0)我來回復

無回覆
  • 取消回覆