从Dumpcap输出

时间:2015-07-14 19:27:14

标签: python parsing rhel6

我正在尝试解析linux中终端中运行dumpcap的输出的文件名,以便自动将其附加到电子邮件中。这是较大脚本的相关功能。 proc1stdouteventfile已初始化为""DUMPCAP为命令行字符串dumpcap -a duration:300 -b duration:2147483647 -c 500 -i 1 -n -p -s 2 -w test -B 20

def startdump():                       
    global DUMPCAP                     
    global dumpdirectory               
    global proc1                       
    global stdout                      
    global eventfile                   
    setDumpcapOptions()                
    print("dumpcap.exe = " + DUMPCAP)  
    os.chdir(dumpdirectory)            
    #subprocess.CREATE_NEW_CONSOLE     
    proc1 = subprocess.Popen(DUMPCAP, shell=True, stdout=subprocess.PIPE)
    for line in proc1:                 
        if 'File: ' in line:           
            parsedfile = line.split(':')
            eventfile = parsedfile[1]  
    if dc_mode == "Dumpcap Only":      
        proc1.communicate()            
        mail_man(config_file)          
     return proc1                       

def startevent():
    global EVENT
    global proc1
    global eventfile
    setEventOptions()
    print(EVENT)
    # subprocess.CREATE_NEW_CONSOLE
    proc2 = subprocess.Popen(EVENT, shell=True, stdout=subprocess.PIPE) 
    if dc_mode == "Dumpcap+Event" or dc_mode == "Trigger" or dc_mode == "Event Only":
        proc2 = proc1.communicate()
        mail_man(config_file)
    return proc2

我一直遇到的问题是我无法弄清楚如何从dumpcap的输出中解析文件名。无论我做什么,它都会从输出中解析""。如果这似乎没有被研究,我道歉。我有一个月的时间来学习python和linux,而且文档很简洁,也很麻烦。

我应该创建一个函数来解析dumpcap输出中的事件文件,还是在脚本中直接执行?我真的在这里不知所措。我不确定dumpcap如何存储它的输出。

终端中的dumcap输出为:

dumpcap.exe = dumpcap -a duration:300 -b duration:2147483647 -c 500 -i 1 -n -p -s 2 -w test -B 20
 -i 1 - f icmp and host 156.24.31.29 - c 2
/bin/sh: -i: command not found
Capturing on 'eth0'
File: test_00001_20150714141827
Packets captured: 500
Packets received/dropped on interface 'eth0': 500/0 (pcap:0/dumpcap:0/flushed:0/ps_ifdrop:0) (100.0%)
[Errno 2] No such file or directory: ''

File: ...包含由pcap保存的dumpcap文件的随机生成名称我试图从终端解析该行以获取File:之后的所有内容设置为变量,但传统的。split方法似乎不起作用

它给出的另一个错误是Popen无法编入索引

2 个答案:

答案 0 :(得分:0)

看起来基本上你需要一个正则表达式。

import re
rx = re.compile('^File: (\S+)$', re.MULTILINE)
m = rx.search(stdout_contents)
if m:
  file_name = m.group(1)
# else: file name not detected.

更新:读取管道标准输出的最小完整示例;希望这有帮助。

import subprocess
proc = subprocess.Popen("echo foo", shell=True, stdout=subprocess.PIPE)
result = proc.communicate()
print result[0]  # must be `foo\n`

答案 1 :(得分:0)

Dumpcap输出到stderr而不是stdout。所以我设法将stderr重定向到txt文件,然后我可以解析它。

def startdump():
    global DUMPCAP, dumpdirectory, proc1
    global eventfile, dc_capfile

    DUMPCAP = ''
    print("========================[ MAIN DUMPCAP MONITORING ]===========================")
    setDumpcapOptions()
    os.chdir(dumpdirectory)
    if platform == "Linux":
        DUMPCAP = "dumpcap " + DUMPCAP
    elif platform == "Windows":
        DUMPCAP = "dumpcap.exe " + DUMPCAP
    proc1 = subprocess.Popen(DUMPCAP, shell=True, stderr=subprocess.PIPE)
    #procPID = proc1.pid
    if dc_mode == "Dumpcap Only":
        time.sleep(5)
        with open("proc1stderr.txt", 'w+') as proc1stderr:
            proc1stderr.write(str(proc1.stderr))
            for line in proc1.stderr:
                print("%s" % line)
                if "File:" in line:
                    print(line)
                    raweventfile = line.split('File: ')[1]
                    eventfile = raweventfile.strip('\[]').rstrip('\n')
                    mail_man()
            proc1.communicate()