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



本文转自 lover00751CTO博客,原文链接:http://blog.51cto.com/wangwei007/1212492,如需转载请自行联系原作者

相关文章
|
2月前
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
295 7
|
2月前
|
监控 安全 程序员
Python日志模块配置:从print到logging的优雅升级指南
从 `print` 到 `logging` 是 Python 开发的必经之路。`print` 调试简单却难维护,日志混乱、无法分级、缺乏上下文;而 `logging` 支持级别控制、多输出、结构化记录,助力项目可维护性升级。本文详解痛点、优势、迁移方案与最佳实践,助你构建专业日志系统,让程序“有记忆”。
225 0
|
2月前
|
JSON 算法 API
Python中的json模块:从基础到进阶的实用指南
本文深入解析Python内置json模块的使用,涵盖序列化与反序列化核心函数、参数配置、中文处理、自定义对象转换及异常处理,并介绍性能优化与第三方库扩展,助你高效实现JSON数据交互。(238字)
340 4
|
2月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
253 0
|
2月前
|
XML JSON 数据处理
超越JSON:Python结构化数据处理模块全解析
本文深入解析Python中12个核心数据处理模块,涵盖csv、pandas、pickle、shelve、struct、configparser、xml、numpy、array、sqlite3和msgpack,覆盖表格处理、序列化、配置管理、科学计算等六大场景,结合真实案例与决策树,助你高效应对各类数据挑战。(238字)
177 0
|
3月前
|
安全 大数据 程序员
Python operator模块的methodcaller:一行代码搞定对象方法调用的黑科技
`operator.methodcaller`是Python中处理对象方法调用的高效工具,替代冗长Lambda,提升代码可读性与性能。适用于数据过滤、排序、转换等场景,支持参数传递与链式调用,是函数式编程的隐藏利器。
119 4
|
3月前
|
存储 数据库 开发者
Python SQLite模块:轻量级数据库的实战指南
本文深入讲解Python内置sqlite3模块的实战应用,涵盖数据库连接、CRUD操作、事务管理、性能优化及高级特性,结合完整案例,助你快速掌握SQLite在小型项目中的高效使用,是Python开发者必备的轻量级数据库指南。
282 0
|
4月前
|
存储 安全 数据处理
Python 内置模块 collections 详解
`collections` 是 Python 内置模块,提供多种高效数据类型,如 `namedtuple`、`deque`、`Counter` 等,帮助开发者优化数据处理流程,提升代码可读性与性能,适用于复杂数据结构管理与高效操作场景。
327 0
|
5月前
|
数据安全/隐私保护 Python
抖音私信脚本app,协议私信群发工具,抖音python私信模块
这个实现包含三个主要模块:抖音私信核心功能类、辅助工具类和主程序入口。核心功能包括登录
|
8月前
|
Python
Python教程:os 与 sys 模块详细用法
os 模块用于与操作系统交互,主要涉及夹操作、路径操作和其他操作。例如,`os.rename()` 重命名文件,`os.mkdir()` 创建文件夹,`os.path.abspath()` 获取文件绝对路径等。sys 模块则用于与 Python 解释器交互,常用功能如 `sys.path` 查看模块搜索路径,`sys.platform` 检测操作系统等。这些模块提供了丰富的工具,便于开发中处理系统和文件相关任务。
341 14

推荐镜像

更多