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,以及如何去访问我们的摄像头.然后我们一起来看看写一个人脸检测程序是如何地简单,简单到只需要几行代码. 在开始之前, ...
随机推荐
- 基于Yarp实现内网http穿透
Yarp介绍 YARP是微软开源的用来代理服务器的反向代理组件,可实现的功能类似于nginx. 基于YARP,开发者可以非常快速的开发一个性能不错的小nginx,用于代理http(s)请求到上游的ht ...
- webpack(2)webpack核心概念
前言 本质上,webpack 是一个用于现代 JavaScript 应用程序的 静态模块打包工具.当 webpack 处理应用程序时,它会在内部构建一个 依赖图(dependency graph) ...
- @Valid 注解的使用
限制 说明 @Null 限制只能为null @NotNull 限制必须不为null @AssertFalse 限制必须为false @AssertTrue 限制必须为true @DecimalMax( ...
- mybatis常用标签(转)
1. 定义sql语句 select 标签 属性介绍: id :唯一的标识符. parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User或user resu ...
- Django基础-002 Models的属性与字段
1.models字段类型 AutoField():一个IntegerField,根据可用ID自动递增.如果没指定主键,就创建它自动设置为主键. IntegerField():一个整数: FloatFi ...
- springMVC-3-获取参数
RequestMapping修饰类 源码: 根据源码可以知道,requestmapping既可以修饰方法也可以修饰类 @Target({ElementType.METHOD, ElementType. ...
- 微信小程序云开发-数据库-列表页携带id跳转到详情页
一.新建页面 新建列表页"pages/goodslist/goodslist",新建列表详情页"pages/gooddetail/gooddetail" 二. ...
- ZYNQ Linux 移植:包含petalinux移植和手动移植debian9
参考: https://electronut.in/workflow-for-using-linux-on-xilinx-zynq/ https://blog.csdn.net/m0_37545528 ...
- 更改Nginx网站根目录以及导致的403 forbidden问题
Nginx采用默认配置,只修改了root的网站根目录位置,再访问网站的时候提示403Forbidden的错误. 仔细检查了新文件夹的权限,也对比了心就网站根目录的权限,都是一样的. 最后尝试关闭了SE ...
- pip3 pip 安装包 临时更换镜像地址
在使用pip3或者pip安装某些第三方包的时候,可能会遇到网络原因导致的安装失败. 可以在安装第三方包的时候临时指定镜像地址. 命令: pip3 install 库名 -i 镜像地址 例如:# pip ...