Logfile zu csv konvertieren
Verfasst: Dienstag 26. März 2019, 13:07
Hallo Leute, ich befasse mich erst seit Neusten mit dem Thema Python.
Mein Ziel ist es ein Logfile zur Auswertung umzuformatieren.
Das ganze funktioniert schon soweit ganz gut, nur ich habe ein Problem das ich das Datum mit der Uhrzeit nicht mitformatiert bekomme.
Das Log hat folgende Einträge.
2019:03:23-00:00:01 hag-fw-ltd-1 ulogd[31638]: id="2001" severity="info" sys="SecureNet" sub="packetfilter" name="Packet dropped" action="drop" fwrule="60002" initf="lag0" outitf="eth6" srcmac="xx:xx:xx:xx:xx:xx" dstmac="xx:xx:xx:xx:xx:xx" srcip="xxx.xxx.xxx.xxx" dstip="xxx.xxx.xxx.xxx" proto="17" length="56" tos="0x00" prec="0x00" ttl="63" srcport="56080" dstport="53"
2019:03:23-00:00:01 hag-fw-ltd-1 ulogd[31638]: id="2002" severity="info" sys="SecureNet" sub="packetfilter" name="Packet accepted" action="accept" fwrule="216" initf="eth1" outitf="lag0" srcmac="xx:xx:xx:xx:xx:xx" dstmac="xx:xx:xx:xx:xx:xx" srcip="xxx.xxx.xxx.xxx" dstip="xxx.xxx.xxx.xxx" proto="6" length="48" tos="0x00" prec="0x00" ttl="247" srcport="17720" dstport="636" tcpflags="SYN"
Das was ich blau markiert habe ist das was mir in dem umformatierten noch fehlt.
Aussehen tut das csv dann wie folgt.
id,severity,sys,sub,name,action,fwrule,initf,srcmac,dstmac,srcip,dstip,proto,length,tos,prec,ttl,srcport,dstport,outitf,tcpflags,info,type,code,app,mark
"2001","info","SecureNet","packetfilter",,"drop","60002","lag0","xx:xx:xx:xx:xx:xx:a5","xx:xx:xx:xx:xx:xx","xxx.xxx.xxx.xxx","xxx.xxx.xxx.xxx","17","56","0x00","0x00","63","56080","53","eth6",,,,,,
"2002","info","SecureNet","packetfilter",,"accept","216","eth1","xx:xx:xx:xx:xx:xx","xx:xx:xx:xx:xx:xx","xxx.xxx.xxx.xxx","xxx.xxx.xxx.xxx","6","48","0x00","0x00","247","17720","636","lag0","SYN",,,
Realisiert habe ich das ganze dann so.
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
POS = { "id": 1 ,"severity": 2 ,"sys": 3 ,"sub": 4
, "name": 5, "action": 6, "fwrule": 7, "initf": 8
, "srcmac": 9, "dstmac": 10, "srcip": 11, "dstip": 12
, "proto": 13, "length": 14, "tos": 15, "prec": 16
, "ttl": 17, "srcport": 18, "dstport": 19, "outitf": 20
, "tcpflags": 21, "info": 22, "type": 23, "code": 24
, "app": 25, "mark": 26
}
## Example of log file
## 2000:01:06-00:00:02 sophos ulogd[293]: id="2014" severity="info" sys="SecureNet" sub="packetfilter" name="DNS...
## 2000:01:06-00:00:02 sophos ulogd[293]: id="2014" severity="info" sys="SecureNet" sub="packetfilter" name="DNS requ...
# Menü for File select
logfilepath = tkFileDialog.askopenfilename(initialdir = "/media/sf_D_DRIVE/log/packetfilter/",title = "Select file",filetypes = (("log files","*.log"),("all files","*.*")))
logfile = open(logfilepath, "r")
csvfilepath = tkFileDialog.asksaveasfilename(initialdir = "/media/sf_D_DRIVE/log/",title = "Select file",filetypes = (("csv files","*.csv"),("all files","*.*")))
csvfile = open(csvfilepath, 'w')
# Print headers in file
k = [""] * len(POS)
for key, value in POS.items():
k[value - 1] = key
csvfile.writelines([",".join(k),'\n'])
# Print contet
for line in logfile:
k = [""] * len(POS)
for m in re.finditer(r'(\w+=\")((\w+)|((\w+:\w+)+)|((\w+\.)+\w+))\"', line):
key = re.search(r'\w+=\"', m.group()).group(0).replace('="', '')
value = re.search(r'\"((\w+)|((\w+:\w+)+)|((\w+\.)+\w+))\"', m.group()).group(0).replace('"', '')
k[POS.get(key) - 1] = '"' + value + '"'
csvfile.writelines([",".join(k),'\n'])
Könnt ihr mir bitte dort einmal helfen?
Danke und Gruß
Oerni
Mein Ziel ist es ein Logfile zur Auswertung umzuformatieren.
Das ganze funktioniert schon soweit ganz gut, nur ich habe ein Problem das ich das Datum mit der Uhrzeit nicht mitformatiert bekomme.
Das Log hat folgende Einträge.
2019:03:23-00:00:01 hag-fw-ltd-1 ulogd[31638]: id="2001" severity="info" sys="SecureNet" sub="packetfilter" name="Packet dropped" action="drop" fwrule="60002" initf="lag0" outitf="eth6" srcmac="xx:xx:xx:xx:xx:xx" dstmac="xx:xx:xx:xx:xx:xx" srcip="xxx.xxx.xxx.xxx" dstip="xxx.xxx.xxx.xxx" proto="17" length="56" tos="0x00" prec="0x00" ttl="63" srcport="56080" dstport="53"
2019:03:23-00:00:01 hag-fw-ltd-1 ulogd[31638]: id="2002" severity="info" sys="SecureNet" sub="packetfilter" name="Packet accepted" action="accept" fwrule="216" initf="eth1" outitf="lag0" srcmac="xx:xx:xx:xx:xx:xx" dstmac="xx:xx:xx:xx:xx:xx" srcip="xxx.xxx.xxx.xxx" dstip="xxx.xxx.xxx.xxx" proto="6" length="48" tos="0x00" prec="0x00" ttl="247" srcport="17720" dstport="636" tcpflags="SYN"
Das was ich blau markiert habe ist das was mir in dem umformatierten noch fehlt.
Aussehen tut das csv dann wie folgt.
id,severity,sys,sub,name,action,fwrule,initf,srcmac,dstmac,srcip,dstip,proto,length,tos,prec,ttl,srcport,dstport,outitf,tcpflags,info,type,code,app,mark
"2001","info","SecureNet","packetfilter",,"drop","60002","lag0","xx:xx:xx:xx:xx:xx:a5","xx:xx:xx:xx:xx:xx","xxx.xxx.xxx.xxx","xxx.xxx.xxx.xxx","17","56","0x00","0x00","63","56080","53","eth6",,,,,,
"2002","info","SecureNet","packetfilter",,"accept","216","eth1","xx:xx:xx:xx:xx:xx","xx:xx:xx:xx:xx:xx","xxx.xxx.xxx.xxx","xxx.xxx.xxx.xxx","6","48","0x00","0x00","247","17720","636","lag0","SYN",,,
Realisiert habe ich das ganze dann so.
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
POS = { "id": 1 ,"severity": 2 ,"sys": 3 ,"sub": 4
, "name": 5, "action": 6, "fwrule": 7, "initf": 8
, "srcmac": 9, "dstmac": 10, "srcip": 11, "dstip": 12
, "proto": 13, "length": 14, "tos": 15, "prec": 16
, "ttl": 17, "srcport": 18, "dstport": 19, "outitf": 20
, "tcpflags": 21, "info": 22, "type": 23, "code": 24
, "app": 25, "mark": 26
}
## Example of log file
## 2000:01:06-00:00:02 sophos ulogd[293]: id="2014" severity="info" sys="SecureNet" sub="packetfilter" name="DNS...
## 2000:01:06-00:00:02 sophos ulogd[293]: id="2014" severity="info" sys="SecureNet" sub="packetfilter" name="DNS requ...
# Menü for File select
logfilepath = tkFileDialog.askopenfilename(initialdir = "/media/sf_D_DRIVE/log/packetfilter/",title = "Select file",filetypes = (("log files","*.log"),("all files","*.*")))
logfile = open(logfilepath, "r")
csvfilepath = tkFileDialog.asksaveasfilename(initialdir = "/media/sf_D_DRIVE/log/",title = "Select file",filetypes = (("csv files","*.csv"),("all files","*.*")))
csvfile = open(csvfilepath, 'w')
# Print headers in file
k = [""] * len(POS)
for key, value in POS.items():
k[value - 1] = key
csvfile.writelines([",".join(k),'\n'])
# Print contet
for line in logfile:
k = [""] * len(POS)
for m in re.finditer(r'(\w+=\")((\w+)|((\w+:\w+)+)|((\w+\.)+\w+))\"', line):
key = re.search(r'\w+=\"', m.group()).group(0).replace('="', '')
value = re.search(r'\"((\w+)|((\w+:\w+)+)|((\w+\.)+\w+))\"', m.group()).group(0).replace('"', '')
k[POS.get(key) - 1] = '"' + value + '"'
csvfile.writelines([",".join(k),'\n'])
Könnt ihr mir bitte dort einmal helfen?
Danke und Gruß
Oerni