在python中通过Regex匹配Popen输出

问题描述 投票:0回答:1

我正在使用Python Popen执行命令并将结果捕获到变量中:

av_proc = Popen(
        [
            CLAMSCAN_PATH,
            "--stdout",
            "-d",
            AV_DEFINITION_PATH,
            blob_full_path
        ],
        stderr=STDOUT,
        stdout=PIPE,
        env=av_env
        )
    communicateArray = av_proc.communicate()
    claimav_output = communicateArray[0].decode('utf-8')        
    logging.info("clamAV output {}".format(claimav_output))
    logging.info("clamAV output Type {}".format(type(claimav_output)))

打印的日志行是:

[3/1/19 6:38:01 AM] clamAV output LibClamAV Warning: **************************************************
[3/1/19 6:38:01 AM] LibClamAV Warning: ***  The virus database is older than 7 days!  ***
[3/1/19 6:38:01 AM] LibClamAV Warning: ***   Please update it as soon as possible.    ***
[3/1/19 6:38:01 AM] LibClamAV Warning: **************************************************
[3/1/19 6:38:01 AM] /home/admin/Desktop/blob_folder/test2: Worm.Mydoom.I FOUND
[3/1/19 6:49:28 AM] clamAV output Type <class 'str'>

我的目标是在此输出中提取病毒名称,即“Worm.Mydoom.I”。

输出将始终采用以下格式:

文件路径:Detection_Name FOUND

我编写了以下python脚本来提取检测名称:

    matchObj = re.match(r'(?<=:\s)\S+(?=\s+FOUND)', claimav_output)
    logging.info("matchObj  Type {}".format(type(matchObj)))
    logging.info(matchObj.group())
    logging.info(matchObj)

不幸的是,这不起作用。有人能指出我的代码中的错误。这是我在日志中看到的错误:

[3/1/19 6:38:01 AM] clamAV output Type <class 'str'>
[3/1/19 6:38:01 AM] CLAIMAV Parsing claimav output to extract any scan resullt 
[3/1/19 6:38:01 AM] matchObj  Type <class 'NoneType'>
[3/1/19 6:38:01 AM] 'NoneType' object has no attribute 'group'
[3/1/19 6:38:01 AM] Something went wrong
python regex python-3.6 popen
1个回答
1
投票

您可以使用环绕声来使用此正则表达式捕获病毒名称,

(?<=:\s)\S+(?=\s+FOUND)

Demo

看看这个Python代码,

import re

s = """[3/1/19 6:02:52 AM] clamAV output b'LibClamAV Warning: **************************************************\nLibClamAV Warning: * The virus database is older than 7 days! \nLibClamAV Warning: Please update it as soon as possible. *\nLibClamAV Warning: **************************************************\n/home/admin/Desktop/blob_folder/test2: Worm.Mydoom.I FOUND\n\n----------- SCAN SUMMARY -----------\nKnown viruses: 3832461\nEngine version: 0.100.2\nScanned directories: 0\nScanned files: 1\nInfected files: 1\nData scanned: 0.02 MB\nData read: 0.02 MB (ratio 1.00:1)\nTime: 6.519 sec (0 m 6 s)\n'

    [3/1/19 6:02:52 AM] clamAV output Type class 'bytes'"""

m = re.search(r'(?<=:\s)\S+(?=\s+FOUND)', s)
if(m):
 print(m.group())

打印病毒名称,

Worm.Mydoom.I
© www.soinside.com 2019 - 2024. All rights reserved.