A Python Environment Detector
The user provide the method to get result(command on remote host), the check standard(a callback function), and information about target host(ip and username), and a timeout of execution time optional, with the envdet module, you can get the result: if the command output obey the check standard.
The application module, detapp.py:
import logging
from envdet import rcmd
logger = logging.getLogger('DetectApp')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('detect.log')
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
logger.addHandler(ch)
logger.addHandler(fh)
def isOracleJDK(str):
return 'Java(TM)' in str
res = rcmd('bvt', '10.0.2.47', 'java -version', isOracleJDK)
logger.info('Check result:%s' % res)
The environment detection module, envdet.py:
from subprocess import Popen, PIPE, STDOUT
import signal
import logging
logger = logging.getLogger('DetectApp.envdet')
def handler(signum, frame):
logger.error('Signal handler called with signal: %d' % signum)
raise IOError("Command execution timeout!")
def rcmd(user, host, cmd, check_handler, timeout=10):
signal.signal(signal.SIGALRM, handler)
signal.alarm(timeout)
cmdstr = "ssh %s@%s 'source /etc/profile;%s'" % (user, host, cmd)
p = Popen(cmdstr, close_fds=True, shell=True, stdout=PIPE, stderr=STDOUT)
fullres = line = ''
while p.poll() is None:
out = p.stdout.read(1)
fullres = fullres + out
if out=='\n':
logger.debug(line)
line = ''
else:
line = line + out
logger.debug('----ret of cmd %s is: %d----' % (cmd, p.returncode))
return check_handler(fullres)
The technical points here are:
Run shell command in Python and retrieve output and return code;
Use signal to limit the overall running time of commands on remote host over SSH;
The logging utility across multiple modules, notice the naming rules: .. So if you rename the module name, rename it's logger accordingly.
A Python Environment Detector的更多相关文章
- Prepare Python environment and install selenium.
1, Install python and selenium. I use python 3.5, the following is the example 1.) Python downloa ...
- Create your first isolated Python environment
# Install virtualenv for Python 2.7 and create a sandbox called my27project: pip2. install virtualen ...
- centos python environment
3. 在Centos7的docker里装好了httpd,运行报错: $ systemctl start httpd.service Failed to get D-Bus connection: Op ...
- visual env VS conda environment of python
1. There's two types of python environment in pycharm: virtualenv Environment conda environment For ...
- Python框架、库以及软件资源汇总
转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...
- Awesome Python
Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Insp ...
- Machine and Deep Learning with Python
Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...
- Install OpenCV 3.0 and Python 2.7+ on OSX
http://www.pyimagesearch.com/2015/06/15/install-OpenCV-3-0-and-Python-2-7-on-osx/ As I mentioned las ...
- OpenCV-Python(1)在Python中使用OpenCV进行人脸检测
OpenCV是如今最流行的计算机视觉库,而我们今天就是要学习如何安装使用OpenCV,以及如何去访问我们的摄像头.然后我们一起来看看写一个人脸检测程序是如何地简单,简单到只需要几行代码. 在开始之前, ...
随机推荐
- Linux从头学02:x86中内存【段寻址】方式的来龙去脉
作 者:道哥,10+年的嵌入式开发老兵. 公众号:[IOT物联网小镇],专注于:C/C++.Linux操作系统.应用程序设计.物联网.单片机和嵌入式开发等领域. 公众号回复[书籍],获取 Linux. ...
- LAMP——实现phpMyadmin、wordpress及Discuz应用部署
一.环境准备 操作系统:Centos8.3.2011 软件:Apache2.4.37.Mysql8.0.21.PHP7.2.24 二.安装过程 1.安装phpmyadmin 1.1.安装软件包并启动服 ...
- Binding(五):多路绑定
Binding不止能绑定一个源,它还能绑定多个源,这就是我们这节要讲的多路绑定:MultiBinding. 使用多路绑定跟一般的绑定还是有区别的,首先它并不能很好的在标记扩展中使用,另外,使用多路绑定 ...
- 01_JVM与Java体系结构
JVM发展历程 Sun Classic VM Exact VM 为了解决上一个虚拟机问题,jdk1.2时,sun提供了此虚拟机. Exact Memory Management:准确式内存管理 SUN ...
- 17 shell break与continue
使用 while.until.for.select 循环时,如果想提前结束循环(在不满足结束条件的情况下结束循环),可以使用 break 或者 continue 关键字. 在C语言.Python.Ja ...
- 深入理解 PHP7 中全新的 zval 容器和引用计数机制
深入理解 PHP7 中全新的 zval 容器和引用计数机制 最近在查阅 PHP7 垃圾回收的资料的时候,网上的一些代码示例在本地环境下运行时出现了不同的结果,使我一度非常迷惑. 仔细一想不难发现问题所 ...
- .NET Core/.NET5/.NET6 开源项目汇总11:WPF组件库1
系列目录 [已更新最新开发文章,点击查看详细] WPF(Windows Presentation Foundation)是微软推出的基于Windows 的用户界面框架,属于.NET Frame ...
- F5负载均衡-配置指导手册(含IPv6)
F5负载均衡-配置手册 设备概况 图形化界面 通过网络形式访问F5任一接口地址,打开浏览器输入https://网络接口地址:或pc机直连F5的MGMT带外管理口,打开浏览器,输入https://192 ...
- 3.Java入门
一.Java帝国的诞生 一场旷日持久的战争 1.C & C++ 1972年C诞生 贴近硬件(有汇编的一些特点),运行极快,效率极高 操作系统,编译器,数据库,网络系统等 指针(能够直接操作内存 ...
- 使用vlookup的模糊匹配和字符串拼接
1,=IF(ISNA(VLOOKUP("*"&$D2&"*",$A$2:$A$43,1,FALSE))=FALSE,TRUE,FALSE) 2, ...