2016-04-05 8 views
0

Hallo Ich suche eine McAfee-Protokolldatei zu trimmen und entfernen Sie alle "ist OK" und andere gemeldete Instanzen, die ich nicht interessiert. Bevor wir ein Shell-Skript verwendeten, das die Option -v für grep ausnutzte, wollen wir nun ein Python-Skript schreiben, das sowohl auf Linux als auch auf Windows funktioniert. Nach ein paar Versuchen konnte ich eine Regex in einem Online-Regex-Builder verwenden, aber es fällt mir schwer, sie in mein Skript zu implementieren. Online REGEX BuilderInverse-Match-Hilfe in Python

Edit: Ich möchte, dass die entfernen „ist OK“, „ist ein broken“, „ist ein Blocklinien“, und Linien „Datei konnte nicht geöffnet werden“, so dann bin ich nur mit einer Datei links nur die Probleme, die ich interessiert bin Eine Art von dergleichen wie dies in der Schale.

grep -v "is OK" ${OUTDIR}/${OUTFILE} | grep -v "is a broken" | grep -v "file could not be opened" | grep -v "is a block" > ${OUTDIR}/${OUTFILE}.trimmed 2>&1 

ich lese in und durch die Datei suchen hier:

import re 

f2 = open(outFilePath) 
contents = f2.read() 
print contents 
p = re.compile("^((?!(is OK)|(file could not be opened)| (is a broken)|(is a block)))*$", re.MULTILINE | re.DOTALL) 
m = p.findall(contents) 
print len(m) 
for iter in m: 
    print iter 
f2.close() 

Eine Probe der Datei ich bin versuchen zu suchen:

eth0 
10.0.11.196 
00:0C:29:AF:6A:A7 
parameters passed to uvscan: --DRIVER /opt/McAfee/uvscan/datfiles/current -- ANALYZE --AFC=32 ATIME-PRESERVE --PLAD --RPTALL RPTOBJECTS SUMMARY --UNZIP -- RECURSIVE --SHOWCOMP --MIME --THREADS=4 /tmp 
temp XML output is: /tmp/HIQZRq7t2R 
McAfee VirusScan Command Line for Linux64 Version: 6.0.5.614 
Copyright (C) 2014 McAfee, Inc. 
(408) 988-3832 LICENSED COPY - April 03 2016 

AV Engine version: 5700.7163 for Linux64. 
Dat set version: 8124 created Apr 3 2016 
Scanning for 670707 viruses, trojans and variants. 


No file or directory found matching /root/SVN/swd-lhn-build/trunk/utils/ATIME-PRESERVE 

No file or directory found matching /root/SVN/swd-lhn-build/trunk/utils/RPTOBJECTS 

No file or directory found matching /root/SVN/swd-lhn-build/trunk/utils/SUMMARY 
/tmp/tmp.BQshVRSiBo ... is OK. 
/tmp/keyring-F6vVGf/socket ... file could not be opened. 
/tmp/keyring-F6vVGf/socket.ssh ... file could not be opened. 
/tmp/keyring-F6vVGf/socket.pkcs11 ... file could not be opened. 
/tmp/yum.log ... is OK. 
/tmp/tmp.oW75zGUh4S ... is OK. 
/tmp/.X11-unix/X0 ... file could not be opened. 
/tmp/tmp.LCZ9Ji6OLs ... is OK. 
/tmp/tmp.QdAt1TNQSH ... is OK. 
/tmp/ks-script-MqIN9F ... is OK. 
/tmp/tmp.mHXPvYeKjb/mcupgrade.conf ... is OK. 
/tmp/tmp.mHXPvYeKjb/uvscan/uninstall-uvscan ... is OK. 
/tmp/tmp.mHXPvYeKjb/mcscan ... is OK. 
/tmp/tmp.mHXPvYeKjb/uvscan/install-uvscan ... is OK. 
/tmp/tmp.mHXPvYeKjb/uvscan/readme.txt ... is OK. 
/tmp/tmp.mHXPvYeKjb/uvscan/uvscan_secure ... is OK. 
/tmp/tmp.mHXPvYeKjb/uvscan/signlic.txt ... is OK. 
/tmp/tmp.mHXPvYeKjb/uvscan/uvscan ... is OK. 
/tmp/tmp.mHXPvYeKjb/uvscan/liblnxfv.so.4 ... is OK. 

Aber ich bekomme nicht die richtige Ausgabe. Ich habe versucht, sowohl die MULTILINE- als auch die DOTALL-Optionen zu entfernen und bekomme immer noch nicht die richtige Antwort. Unten ist die Ausgabe, wenn mit DOTALL und MULTILINE ausgeführt wird.

9 
('', '', '', '', '') 
('', '', '', '', '') 
('', '', '', '', '') 
('', '', '', '', '') 
('', '', '', '', '') 
('', '', '', '', '') 
('', '', '', '', '') 
('', '', '', '', '') 
('', '', '', '', '') 

Jede Hilfe würde sehr geschätzt werden !! Vielen Dank!!

+0

Mit 're.findall' extrahieren Sie alle erfassten Werte. Probieren Sie 're.compile (r '^ (?: (?! \ B (?: Ist OK | Datei konnte nicht geöffnet werden | ist ein Fehler | ist ein Block) \ b).) + $', Re.DOTALL | re.MULTILINE) 'mit' re.findall' –

+0

Können Sie zeigen, was Sie Zielausgabe ist, werde ich es sofort lösen. –

+0

Ich denke, ich war nicht klar, ich möchte alle Zeilen, die nicht enden mit "ist OK" oder "ist eine kaputte" Art wie wenn ich laufen würde: grep -v "ist OK" $ {OUTDIR}/$ {OUTFILE} | grep -v "ist ein kaputtes" | grep -v "Datei konnte nicht geöffnet werden" | grep -v "ist ein Block"> $ {OUTDIR}/$ {OUTFILE} .trimmed 2> & 1 –

Antwort

2

denken vielleicht einfacher, Zeile für Zeile:

import re 
import sys 

pattern = re.compile(r"(is OK)|(file could not be opened)|(is a broken)|(is a block)") 

with open(sys.argv[1]) as handle: 
    for line in handle: 
     if not pattern.search(line): 
      sys.stdout.write(line) 

Ausgänge:

eth0 
10.0.11.196 
00:0C:29:AF:6A:A7 
parameters passed to uvscan: --DRIVER /opt/McAfee/uvscan/datfiles/current -- ANALYZE --AFC=32 ATIME-PRESERVE --PLAD --RPTALL RPTOBJECTS SUMMARY --UNZIP -- RECURSIVE --SHOWCOMP --MIME --THREADS=4 /tmp 
temp XML output is: /tmp/HIQZRq7t2R 
McAfee VirusScan Command Line for Linux64 Version: 6.0.5.614 
Copyright (C) 2014 McAfee, Inc. 
(408) 988-3832 LICENSED COPY - April 03 2016 

AV Engine version: 5700.7163 for Linux64. 
Dat set version: 8124 created Apr 3 2016 
Scanning for 670707 viruses, trojans and variants. 


No file or directory found matching /root/SVN/swd-lhn-build/trunk/utils/ATIME-PRESERVE 

No file or directory found matching /root/SVN/swd-lhn-build/trunk/utils/RPTOBJECTS 

No file or directory found matching /root/SVN/swd-lhn-build/trunk/utils/SUMMARY 
+0

Ich dachte, er sagte, er wollte diese Zeilen entfernen. Er will alles andere. –

+0

Ja, ich denke du willst 'wenn pattern.search (line) None' ist (oder' falls nicht pattern.search (line) '). – Blckknght

+0

@CPanda, klar, eine triviale Lösung, wenn der Code einfach genug ist! – cdlane

0

Manchmal reguläre Ausdrücke sind komplizierter, aber wenn Sie wirklich nur für diesen Mustern suchen, dann würde ich wahrscheinlich Versuchen Sie einfach die einfache Methode:

Es kann nicht schneller sein als die Regex, aber es ist etwa so einfach wie es geht. Alternativ können Sie auch einen etwas strengeren Ansatz verwenden:

terms = (
    'is a broken', 
    'is a block', 
) 

with open('/tmp/samplelog.log') as f: 
    for line in f: 
     line = line.strip() 
     if not line: 
      continue 
     elif line.endswith('is OK.'): 
      continue 
     elif line.endswith('file could not be opened.'): 
      continue 
     elif any(term in line for term in terms): 
      continue 
     print(line) 

Der Ansatz, den ich nehmen würde, hängt weitgehend von der ich erwarte das Skript zu verwenden :)

0

Versuchen Sie, diese (und es wird in einer Zeile durchgeführt)

p = re.compile("^(?:[if](?!s OK|s a broken|s a block|ile could not be opened)|[^if])*$") 

es bedeutet, dass, wenn in einer Zeile, die Sie ein „i“ oder ein „f“ nicht das erwähnte Suffix gefolgt werden kann, oder es ist kein „i“ oder „f“ dann ok es ist. Es wiederholt das für alle Charater in der Linie.

Edit: Nach dem Testen auf regex101.com, fand ich, warum es nicht funktionierte. Hier ist die Ein-Zeilen-Regex, die funktioniert.

p = re.compile("^(?:[^if\n]|[if](?!s OK|ile could not be openeds OK|s a broken|s a block|ile could not be opened))*$", re.MULTILINE) 
+0

Ich habe deine Lösung versucht, aber kein Glück. Ich bin mir nicht sicher, was ich falsch mache ... –

+0

Wie viele ergeben sich mit diesem Muster? –

+0

Ich denke, es mag nicht die '(?: OK | sa ...' Ich nahm es heraus. –