python paramiko模塊中設置執行命令超時值

   常常使用paramiko工具對幾百臺設備進行管理,可是因爲服務器自己或是網絡緣由,有時返回值回不來,而後程序就看在那裏一直等待,這個時候後須要設置一個超時值。paramiko模塊中執行命令代碼以下:python

stdin, stdout , stderr = s.exec_command(command)服務器

   這個地方在模塊中只有一個參數,paramiko默認在這個是並不能設置超時值。網絡

其實paramiko自己是能夠在這個地方設置超時值的,只是默認狀況下是沒有這個選項的,須要在paramiko的安裝目錄中修改他的源代碼,讓他支持,在代碼中是有這個接口的。之因此他沒有這個這個超時值,我想是由於開發方考慮有些有些命令可能執行的時間比較長,好比大文件的壓縮等,須要很長的時間才能執行完,超時值若是設置的話,有可能會中斷命令的執行,索性留下接口,並不設置超時值。可是咱們用這個模塊批量的去操做多臺設備的話,有時超時值是頗有必要的。session

修改paramiko源代碼方法以下:ide

找到C:\Python27\Lib\site-packages\paramiko目錄,下面有個client.py文件,文件中找到這段代碼:工具

def exec_command(self, command, bufsize=-1):
    """
    Execute a command on the SSH server.  A new L{Channel} is opened and
    the requested command is executed.  The command's input and output
    streams are returned as python C{file}-like objects representing
    stdin, stdout, and stderr.
    @param command: the command to execute
    @type command: str
    @param bufsize: interpreted the same way as by the built-in C{file()} function in python
    @type bufsize: int
    @return: the stdin, stdout, and stderr of the executing command
    @rtype: tuple(L{ChannelFile}, L{ChannelFile}, L{ChannelFile})
    @raise SSHException: if the server fails to execute the command
    """
    chan = self._transport.open_session()
    chan.exec_command(command)
    stdin = chan.makefile('wb', bufsize)
    stdout = chan.makefile('rb', bufsize)
    stderr = chan.makefile_stderr('rb', bufsize)
    return stdin, stdout, stderr

修改成:ui

def exec_command(self, command, bufsize=-1,timeout = None):
    """
    Execute a command on the SSH server.  A new L{Channel} is opened and
    the requested command is executed.  The command's input and output
    streams are returned as python C{file}-like objects representing
    stdin, stdout, and stderr.
    @param command: the command to execute
    @type command: str
    @param bufsize: interpreted the same way as by the built-in C{file()} function in python
    @type bufsize: int
    @return: the stdin, stdout, and stderr of the executing command
    @rtype: tuple(L{ChannelFile}, L{ChannelFile}, L{ChannelFile})
    @raise SSHException: if the server fails to execute the command
    """
    chan = self._transport.open_session()
    if timeout is not None:
        chan.settimeout(timeout)
    chan.exec_command(command)
    stdin = chan.makefile('wb', bufsize)
    stdout = chan.makefile('rb', bufsize)
    stderr = chan.makefile_stderr('rb', bufsize)
    return stdin, stdout, stderr

主要就修改了兩個地方:spa

一、def exec_command(self, command, bufsize=-1,timeout = None)定義時加一個timeout = None;server

二、在chan = self._transport.open_session()下面添加一個判斷接口

if timeout is not None:

   chan.settimeout(timeout)


那麼在使用paramiko模塊執行命令時的代碼以下:

stdin, stdout , stderr = s.exec_command(command, timeout=10)

這樣就有一個超時值,執行命令的超時時間爲10s

相關文章
相關標籤/搜索