我正在使用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”。
输出将始终采用以下格式:
文件路径:发现检测名称
我编写了以下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
答案 0 :(得分:1)
您可以使用此正则表达式使用环顾功能捕获病毒名称,
box-sizing: border-box
查看此Python代码,
(?<=:\s)\S+(?=\s+FOUND)
打印病毒名称,
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())