-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_srx_logs.py
69 lines (60 loc) · 2.75 KB
/
parse_srx_logs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python3
# Parse Juniper SRX traffic logs and write into a CSV file
#
import csv
import re
import os
import sys
# csv hdr
header = ["Source Address", "Destination Address", "Protocol", "Port", "Source zone", "Destination zone", "Application name", "Policy name"]
# utility functions
def convertPortoNum(proto_id):
return {
1: "ICMP",
6: "TCP",
17: "UDP",
53: "UDP",
}.get(int(proto_id), "Undefined Protocol")
def removeQuotes(strName):
return strName.split("=")[1].strip('"')
def convertToList(string):
return list(string.split(","))
# main starts
def main():
# get all the files in the dir and read each at a time
cvs_fileName = 'coresrxlogs'+'_'+'res'+'.csv'
dirName = "/root/srx-traffic-logs/core-srx1500"
for fileName in os.listdir(dirName):
with open(os.path.join(dirName, fileName), 'r') as f:
print("Reading the file = ", fileName)
lines = f.readlines()
f.close()
print("Writing into the CSV file = ",cvs_fileName)
with open(os.path.join(dirName, cvs_fileName), 'a', newline = '') as cfile:
writecsv = csv.writer(cfile)
writecsv.writerow(header)
for line in lines:
# session create
if re.match("(.*RT_FLOW_SESSION_CREATE.* )", line, re.I):
pass
noData = line.split('[', 1)[1].split(']')[0]
line_list = list(noData.split(" "))
final_str = removeQuotes(line_list[1]) + ',' + removeQuotes(line_list[3]) + ',' + \
convertPortoNum(removeQuotes(line_list[16])) + ',' + removeQuotes(line_list[4]) + ',' + \
removeQuotes(line_list[18]) + ',' + removeQuotes(line_list[19]) + ',' + \
removeQuotes(line_list[6]) + ',' + removeQuotes(line_list[17])
data = convertToList(final_str)
writecsv.writerow(data)
if re.match("(.*RT_FLOW_SESSION_DENY.* )", line, re.I):
noData = line.split('[', 1)[1].split(']')[0]
line_list = list(noData.split(" "))
final_str = removeQuotes(line_list[1]) + ',' + removeQuotes(line_list[3]) + ',' + \
convertPortoNum(removeQuotes(line_list[7])) + ',' + removeQuotes(line_list[4]) + ',' + \
removeQuotes(line_list[10]) + ',' + removeQuotes(line_list[11]) + ',' + \
removeQuotes(line_list[6]) + ',' + removeQuotes(line_list[9])
data = convertToList(final_str)
writecsv.writerow(data)
# close open files
cfile.close()
if __name__ == "__main__":
main()