python paramiko模块中设置执行命令超时值
2013-05-29 10:45
676 查看
经常使用paramiko工具对几百台设备进行管理,但是由于服务器本身或是网络原因,有时返回值回不来,然后程序就看在那里一直等待,这个时候后需要设置一个超时值。paramiko模块中执行命令代码如下:
stdin, stdout , stderr = s.exec_command(command)
这个地方在模块中只有一个参数,paramiko默认在这个是并不能设置超时值。
其实paramiko本身是可以在这个地方设置超时值的,只是默认情况下是没有这个选项的,需要在paramiko的安装目录中修改他的源代码,让他支持,在代码中是有这个接口的。之所以他没有这个这个超时值,我想是因为开发方考虑有些有些命令可能执行的时间比较长,比如大文件的压缩等,需要很长的时间才能执行完,超时值如果设置的话,有可能会中断命令的执行,索性留下接口,并不设置超时值。但是我们用这个模块批量的去操作多台设备的话,有时超时值是很有必要的。
修改paramiko源代码方法如下:
找到C:\Python27\Lib\site-packages\paramiko目录,下面有个client.py文件,文件中找到这段代码:
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
本文出自 “王伟” 博客,请务必保留此出处http://wangwei007.blog.51cto.com/68019/1212492
stdin, stdout , stderr = s.exec_command(command)
这个地方在模块中只有一个参数,paramiko默认在这个是并不能设置超时值。
其实paramiko本身是可以在这个地方设置超时值的,只是默认情况下是没有这个选项的,需要在paramiko的安装目录中修改他的源代码,让他支持,在代码中是有这个接口的。之所以他没有这个这个超时值,我想是因为开发方考虑有些有些命令可能执行的时间比较长,比如大文件的压缩等,需要很长的时间才能执行完,超时值如果设置的话,有可能会中断命令的执行,索性留下接口,并不设置超时值。但是我们用这个模块批量的去操作多台设备的话,有时超时值是很有必要的。
修改paramiko源代码方法如下:
找到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修改为:
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
本文出自 “王伟” 博客,请务必保留此出处http://wangwei007.blog.51cto.com/68019/1212492
相关文章推荐
- python模块学习之paramiko远程执行命令,文件上传、下载
- Python 2.x如何设置命令执行的超时时间实例
- python模块paramiko实现上传下载与远程执行命令的代码
- Python2.x设置命令执行的超时时间
- python模块paramiko的上传下载和远程执行命令方法
- python模块学习之paramiko远程执行命令,文件上传、下载
- python模块paramiko的上传下载和远程执行命令
- python模块paramiko的上传下载和远程执行命令方法
- python下paramiko模块学习之一:ssh登录和执行命令
- 利用python的paramiko模块ssh登录本地虚拟机并执行任意命令
- Python paramiko模块 实现 ssh远程执行命令 上传下载文件 堡垒机模式下的远程命令执行
- python模块paramiko的上传下载和远程执行命令方法
- python下paramiko模块学习之一:ssh登录和执行命令
- 利用python的paramiko模块,自定义函数远程批量执行shell命令
- python paramiko 向linux执行命令和发送接收文件
- postgresql 设置执行命令超时时间
- python paramiko 执行命令
- python 通过paramiko模块批量执行ssh命令
- 可以设置命令执行的超时时间的脚本
- 利用python fabric模块写的批量操作远程主机脚本(命令执行,上传、下载文件)