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,以及如何去访问我们的摄像头.然后我们一起来看看写一个人脸检测程序是如何地简单,简单到只需要几行代码. 在开始之前, ...
随机推荐
- 跟我一起学Go系列:Go gRPC 安全认证方式-Token和自定义认证
Go gRPC 系列: 跟我一起学Go系列:gRPC安全认证机制-SSL/TLS认证 跟我一起学 Go 系列:gRPC 拦截器使用 跟我一起学 Go 系列:gRPC 入门必备 接上一篇继续讲 gRPC ...
- Redis 底层数据结构之链表
文章参考:<Redis设计与实现>黄建宏 链表 链表提供了高效的节点重排能力,以及可以顺序访问,也可以通过增删节点灵活调整链表长度,Redis中的列表.发布订阅.慢查询.监视器等功能均用到 ...
- CentOS-Docker搭建Nacos-v1.3.2(单点)
说明:从v1.3.1版本开始支持自定义mysql-8.x 通用属性配置(v1.3.2) name description option MODE cluster模式/standalone模式 clus ...
- 创建者模式 -- 单例模式(反射&序列化)
看的视频:https://www.bilibili.com/video/av43896218/?p=286 实现方式:(构造器私有,提供一个外部可以访问的方法(可以提供实例)) 1.饿汉式:线程安全, ...
- 在一个py脚本中调用另外一个py脚本中的类或函数
1.两个文件在同一目录,直接import即可 2.两个文件在不同目录 在导入文件的时候,Python只搜索当前脚本所在的目录,加载(entry-point)入口脚本运行目录和sys.path中包含的路 ...
- ARTS第七周
补上.瞎忙,看来还是效率的问题. 1.Algorithm:每周至少做一个 leetcode 的算法题2.Review:阅读并点评至少一篇英文技术文章3.Tip:学习至少一个技术技巧4.Share:分享 ...
- 分享一份550多个Linux命令的文档,按照命令首字母索引排序
输入一个命令,让我给你一个关于它的完美解释! 众所周知,Linux命令是IT人必须掌握的一个技能,有了它,我们可以部署和维护各种各样的服务和应用.但是,大部分的Linux命令我们不一定记得住,而别是各 ...
- LeetCode解题记录(贪心算法)(二)
1. 前言 由于后面还有很多题型要写,贪心算法目前可能就到此为止了,上一篇博客的地址为 LeetCode解题记录(贪心算法)(一) 下面正式开始我们的刷题之旅 2. 贪心 763. 划分字母区间(中等 ...
- 【Tips】IDEA中自己常用的快捷键
在idea中自己常用的一些快捷键 alt + i; //向上: alt + k; //向下: alt + j; //向左: alt + l; //向右: alt + o; //移至行首: alt + ...
- 【LeetCode】283.移动零
283.移动零 知识点:数组:双指针: 题目描述 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例 输入: [0,1,0,3,12] 输出: [1, ...