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. JS刷新窗口的几种方式

    浮层内嵌iframe及frame集合窗口,刷新父页面的多种方法   <script language=JavaScript>       parent.location.reload(); ...

  2. 2300+字!在不同系统上安装Docker!看这一篇文章就够了

    辰哥准备出一期在Docker跑Python项目的技术文,比如在Docker跑Django或者Flask的网站.跑爬虫程序等等. 在Docker跑Python程序的时候不会太过于细去讲解Docker的基 ...

  3. vim编辑器使用方法(相关指令)

    1.跳到文本的最后一行:按"G",即"shift+g" 2.跳到最后一行的最后一个字符 : 先重复1的操作即按"G",之后按"$& ...

  4. buu 红帽杯easyre

    一.拖入ida静态分析 找到关键函数,然后 这步是可以得出前4个字符是flag,不知道为啥我这边的v15的内存地址为空,不然可以异或解出来的,ida日常抽风... 十次的base64加密,我用在线平台 ...

  5. 每日单词 —— cut to the chase

    cut to the chase: 词面意思--切奶酪 是美国俚语--开门见山:直奔主题 看一看相关的例句或者电影读白吧! 1. - 嫌疑人:Shall we cut to the chase ? 侦 ...

  6. HanLP使用教程——NLP初体验

    话接上篇NLP的学习坑 自然语言处理(NLP)--简介 ,使用HanLP进行分词标注处词性. HanLP使用简介 HanLP是一系列模型与算法组成的NLP工具包,目标是普及自然语言处理在生产环境中的应 ...

  7. 生成Dll在Unity中使用

    我发现很多大佬,插件开发者以及Unity官方都在用Dll来保证既可让使用者正常使用也可有效防止使用者看到自己写的代码 版本说明 Visual Studio版本:2019 16.10.3 Unity版本 ...

  8. P4480 「BJWC2018」「网络流与线性规划24题」餐巾计划问题

    刷了n次用了奇淫技巧才拿到rk1,亥 这道题是网络流二十四题中「餐巾计划问题」的加强版. 于是怀着试一试的心情用费用流交了一发: 哇塞,过了9个点!(强烈谴责出题人用*造数据 下面是费用流解法简述: ...

  9. C语言:printf*("%x")

    #include <stdio.h> int main(){ int a = 100; char str[20] = "hello world!"; char *zza ...

  10. [刘阳Java]_纯CSS代码实现内容过滤效果

    继续我们技术专题课,我们今天给大家带来的是一个比较酷炫的"纯CSS代码实现内容过滤效果",没有加入任何JS的效果.全部都是应用CSS3的新增选择器来实现的.先看效果截图 实现思路 ...