如何使用python re模块过滤Linux命令输出?

时间:2017-11-03 14:11:33

标签: python

我编写了以下用于检测RAID类型的程序。当我运行以下脚本时,re模块与字符串不匹配,并始终给我Raid Type not found输出。

import subprocess
import re

RAIDTYPE = subprocess.Popen('lspci -mm |grep -i "RAID bus controller"', shell=True, stdout=subprocess.PIPE)

output=RAIDTYPE.communicate()[0]

matchObj = re.match(r'(.*) MegaRAID (.*?) .*', output, re.M|re.I)
matchObj1 = re.match(r'(.*) 3ware (.*?) .*', output, re.M|re.I)

if matchObj:
    print("This is MegaRAID")
elif matchObj1:
    print("This is 3Ware Raid")
else:
    print("Raid Type not found")

1 个答案:

答案 0 :(得分:0)

似乎正则表达式不正确。这应该起作用:

re.match(r'.*MegaRAID.*', output, re.M|re.I)

相关问题