python paramiko模块中设置执行命令超时值


经常使用paramiko工具对几百台设备进行管理,但是由于服务器本身或是网络原因,有时返回值回不来,然后程序就看在那里一直等待,这个时候后需要设置一个超时值。paramiko模块中执行命令代码如下:

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

这个地方在模块中只有一个参数,paramiko默认在这个是并不能设置超时值。

其实paramiko本身是可以在这个地方设置超时值的,只是默认情况下是没有这个选项的,需要在paramiko的安装目录中修改他的源代码,让他支持,在代码中是有这个接口的。之所以他没有这个这个超时值,我想是因为开发方考虑有些有些命令可能执行的时间比较长,比如大文件的压缩等,需要很长的时间才能执行完,超时值如果设置的话,有可能会中断命令的执行,索性留下接口,并不设置超时值。但是我们用这个模块批量的去操作多台设备的话,有时超时值是很有必要的。

修改paramiko源代码方法如下:

找到C:\Python27\Lib\site-packages\paramiko目录,下面有个client.py文件,文件中找到这段代码:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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

修改为:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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

主要就修改了两个地方:

1、def exec_command(self, command, bufsize=-1,timeout = None)定义时加一个timeout = None;

2、在chan = self._transport.open_session()下面添加一个判断

if timeout is not None:

chan.settimeout(timeout)

 

那么在使用paramiko模块执行命令时的代码如下:

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

这样就有一个超时值,执行命令的超时时间为10s

相关内容