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的更多相关文章

  1. Prepare Python environment and install selenium.

    1, Install python and selenium. I use python 3.5, the following is the example 1.)    Python downloa ...

  2. Create your first isolated Python environment

    # Install virtualenv for Python 2.7 and create a sandbox called my27project: pip2. install virtualen ...

  3. centos python environment

    3. 在Centos7的docker里装好了httpd,运行报错: $ systemctl start httpd.service Failed to get D-Bus connection: Op ...

  4. visual env VS conda environment of python

    1. There's two types of python environment in pycharm: virtualenv Environment conda environment For ...

  5. Python框架、库以及软件资源汇总

    转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...

  6. Awesome Python

    Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Insp ...

  7. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  8. 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 ...

  9. OpenCV-Python(1)在Python中使用OpenCV进行人脸检测

    OpenCV是如今最流行的计算机视觉库,而我们今天就是要学习如何安装使用OpenCV,以及如何去访问我们的摄像头.然后我们一起来看看写一个人脸检测程序是如何地简单,简单到只需要几行代码. 在开始之前, ...

随机推荐

  1. KDE桌面环境下konsole,kate等软件无法切换中文输入法

    解决方案(arch): 修改/etc/profile,增加以下语句: #fcitxexport XIM_PROGRAM=fcitxexport XIM=fcitxexport GTK_IM_MODUL ...

  2. 4.3Unicode和ASCII码

  3. namenode和datanode启动失败

    1.namenode启动失败,查看错误原因,是无法格式化,再看日志,根据日志提示,清空对应的目录,即可解决这个问题. 2.datanode启动失败: Can't open /var/run/cloud ...

  4. 22 shell组命令与子进程

    1.组命令 2.子进程 2.1 什么是子进程 2.2 创建子进程 2.3 子进程总结 3.如何检测子shell与子进程 1.组命令 组命令,就是将多个命令划分为一组,或者看成一个整体. 用法 区别 S ...

  5. 「CF521D」 Shop

    「CF521D」 Shop 传送门 题目说是有三种操作,首先可以知道赋值操作是可以转化为加法操作的,即 \((1,b) \rightarrow (2,b-a_i)\) 然后加法对于一个数你肯定优先选择 ...

  6. Activiti7 回退与会签

    1.  回退(驳回) 回退的思路就是动态更改节点的流向.先遇水搭桥,最后再过河拆桥. 具体操作如下: 取得当前节点的信息 取得当前节点的上一个节点的信息 保存当前节点的流向 新建流向,由当前节点指向上 ...

  7. SQLITE数据库不支持远程访问

    SQLITE数据库不支持远程访问 import sqlite3 conn=sqlite3.connect("dailiaq.db") cur=conn.cursor() def c ...

  8. HTML元素属性及意义

    HTML属性可以给元素添加附加信息,设置的时候以 (属性名="属性值")成对出现. 属性值应该始终包括在引号内(单引号或双引号),html对大小写不敏感,所以属性和属性值也不区分大 ...

  9. Day10 类与对象-面向对象编程(1)

    面向对象编程(OOP) 面向对象编程的本质就是:以类的方式组织代码,以对象的组织(封装)数据. 抽象 三大特征: 封装 继承 多态 从认识论角度考虑是先有对象后有类.对象,是具体的事物.类,是抽象的, ...

  10. JDK安装与环境搭建.

    卸载JDK 1.删除Java安装目录 2.删除Java Home 3.删除path下Java的目录 4.打开cmd命令输入java-version 出现''不是内部或外部命令,也不是可运行的程序 或批 ...