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

相关文章
相关标签/搜索