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. WEB应用访问缓慢的问题定位

    WEB应用访问缓慢的问题定位 欢迎关注博主公众号「java大师」, 专注于分享Java领域干货文章, 关注回复「资源」, 免费领取全网最热的Java架构师学习PDF, 转载请注明出处 http://w ...

  2. centos 8 下解压.tar.gz文件

    执行命令 tar 参数 文件名 参数: -c :建立一个压缩文件的参数指令(create 的意思): -x :解开一个压缩文件的参数指令: -t :查看 tarfile 里面的文件: 特别注意,在参数 ...

  3. Docker:redis容器使用redis.conf启动失败,不报错

    查看redis.conf配置信息 daemonize no :redis默认是不作为守护进程使用的,这也就是说为什么在你不修改配置文件时直接使用redis-server /redis/redis.co ...

  4. 循序渐进BootstrapVue,开发公司门户网站(6)--- 门户网站后端内容管理

    我们在做门户网站的时候,如果网站的内容可以动态从后端进行管理,那么调整网站内容就非常方便,有时候如一些公司新闻.产品信息.轮播广告信息等都需要动态调整的,有一个方便的后端内容管理是非常方便的.本篇随笔 ...

  5. 软件测试跟踪工具Bugzilla的安装 - Linux版本

    首先查看Linux当前版本 输入"uname -a ",可显示电脑以及操作系统的相关信息 输入"cat /proc/version",说明正在运行的内核版本 输 ...

  6. IO流 connect reset

    目录 出现场景 解决思路 出现场景 通过外部OBS下载10文件,然后通过工具将这10个文件打包成一个文件A.zip上传,最后将这个A.zip下载并解压,解压A.zip后发现文件数量不是10个. 解决思 ...

  7. 前端开发入门到进阶第二集【emmet插件的使用技巧】

    Emmet (前身为 Zen Coding) 是一个能大幅度提高前端开发效率的一个工具.基本上,大多数的文本编辑器都会允许你存储和重用一些代码块,我们称之为"片段".虽然片段能很好 ...

  8. Linux从入门到进阶全集——【第十五集:安装apache服务器】

    1,查看是否安装了httpd软件包以及其依赖:rpm -qa httpd(rpm -qa | grep httpd),如果没有输出任何信息,表示你没有安装httpd软件包,如果有输出一般是已经安装了: ...

  9. 啥是 MySQL 事务隔离级别?

    之前发过一篇文章,简单了解 MySQL 中相关的锁,里面提到了,如果我们使用的 MySQL 存储引擎为 InnoDB ,并且其事务隔离级别是 RR 可重复读的话,是可以避免幻读的. 但是没想到,都 1 ...

  10. FiddlerEverywhere 的配置和基本应用

    一.下载大家自行在官网下载即可,这个可以当做是fiddler的升级版本,里面加了postman的功能,个人感觉界面比较清晰简约,比较喜欢. 二.下载完成之后大家可以自行注册登录,主页面的基本使用如下: ...