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,以及如何去访问我们的摄像头.然后我们一起来看看写一个人脸检测程序是如何地简单,简单到只需要几行代码. 在开始之前, ...
随机推荐
- 【Azure 应用程序见解】Application Insights Java Agent 3.1.0的使用实验,通过修改单个URL的采样率来减少请求及依赖项的数据采集
问题描述 近日好消息,如果是一个Java Spring Cloud的项目,想使用Azure Applicaiton Insights来收集日志及一些应用程序见解.但是有不愿意集成SDK来修改代码或者配 ...
- 用“kill”命令来强制终结某个行为或失常的应用和命令
首先让我们先了解"kill"命令,无论你使用哪种操作系统,你一定会遇到某个行为失常的应用,它把自己锁死并拒绝关闭.在Linux(还有Mac),你可以用一个" ...
- WPF教程十二:了解自定义控件的基础和自定义无外观控件
这一篇本来想先写风格主题,主题切换.自定义配套的样式.但是最近加班.搬家.新租的房子打扫卫生,我家宝宝6月中旬要出生协调各种的事情,导致了最近精神状态不是很好,又没有看到我比较喜欢的主题风格去模仿的, ...
- tr 字符转换命令
tr:可以用来删除一段信息当中的文字,或者是进行文字信息的替换 语法:tr [parameter] set1 ...参数: -d:删除信息当中的set1这个字符 -s:替换掉重复的字符 举例: 将la ...
- 机器学习Sklearn系列:(四)朴素贝叶斯
3--朴素贝叶斯 原理 朴素贝叶斯本质上就是通过贝叶斯公式来对得到类别概率,但区别于通常的贝叶斯公式,朴素贝叶斯有一个默认条件,就是特征之间条件独立. 条件概率公式: \[P(B|A) = \frac ...
- 关于java异常处理的思考
学习java的过程中,初学者更多的是为了实验而写代码,而不考虑实际情况中的人机交互过程中的一些问题. 在java项目中,更多的用户不会因为你给了某些限制提醒,他就一定会按照你所给的提示来输入或者操作, ...
- Java基础00-常用API24
1. Math Math 1.1 Math类概述 1.2 Math类的常用方法 返回绝对值:是正数是时候直接返回参数本身,是负值的时候返回的是参数的相反数.参数是10时返回的是10,参数是-10的时候 ...
- File类与常用IO流第二章过滤器
在第一章中,有一个练习使用递归搜索文件 1 public static void main(String[] args) { 2 File f=new File("E:\\aaa" ...
- 在deeping上安装mariadb
1,安装的官网参考:有安装的命令和指导https://downloads.mariadb.org/mariadb/repositories/#distro=Debian&distro_rele ...
- Jmeter性能测试指标分析
一.Aggregate Report 是 JMeter 常用的一个 Listener,中文被翻译为"聚合报告 如果大家都是做Web应用的性能测试,例如访问百度请求为例,线程10,循环10次, ...