forked from Stunner/FileZilla-Log-Analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFLAmodule.py
200 lines (155 loc) · 5.18 KB
/
FLAmodule.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'''
Created on Jul 10, 2010
FLAmodule version 1.03 alpha
@author: Aaron Jubbal
'''
#!/usr/bin/env python
#Module for parsing FileZilla Server log files
class Error(Exception):
"""Base class for exceptions in this module."""
pass
class InternalError(Error):
"""Raised when situation that is not meant to happen, occurs."""
def __init__(self,errMsg):
self.errMsg = errMsg
def __str__(self):
return repr(self.errMsg)
class G:
nextIndex = 0
arbitraryIndexVal = 1000000
class event(object):
def __init__(self,m,p,d,t,u,i,a,c):
self.message = m #Indicates if line is just a server message instead of an event
self.port = p
self.date = d
self.time = t
self.user = u
self.ip = i
if a[0] == "tuple":
self.action = a[1]
self.ignore = a[2]
self.ignoreFLG = 1
else:
self.action = a
self.ignore = 0
self.ignoreFLG = 0
self.line = c+1
#PRIVATE FUNCTIONS:
#remove beginning and ending parenthesis and convert to string
def _parseIP(IPstring):
return IPstring[1:len(IPstring)-2]
#remove 0th and 7th element(parentheses) and convert to int
def _parsePort(portString):
#print portString
return int(portString[1:7])
#recursively join strings until ip address encountered
def _parseUsr(i,fline):
if fline[i][0] == '(': #if next element is ip address
G.nextIndex = i
return fline[i-1]
else:
return ' '.join((fline[i-1],_parseUsr(i+1,fline)))
#recursively join strings until newline is reached
def _parseAction(i,fline):
if fline[i] == "\n":
return ''
if fline[i+1] == "\n":
return fline[i]
else:
return ' '.join((fline[i],_parseAction(i+1,fline)))
def findIndexOfItem(item,list):
for i in range(len(list)):
if item == list[i]:
return i
return G.arbitraryIndexVal
#parses entire line
def _parse(line):
fline = line.split()
fline.append('\n') #added to indicate when line ends
#print fline
if fline[0] == "FileZilla" or fline[0] == "Initializing" or \
fline[0] == "Creating" or fline[0] == "Server" or \
fline[0] == "Closing" or fline[0] == "Listen" or fline[0] == "Failed":
G.nextIndex = 0
m = 1 #line is a message
a = _parseAction(G.nextIndex,fline) #action
return (m,-1,'---','---','---','---',a)
else:
G.nextIndex = 0
m = 0 #line is not a message i.e. contains data to parse
p = _parsePort(fline[0]) #port number
d = fline[1] #date
t = fline[2] #time
indexOfHyphen = findIndexOfItem('-',fline)
if indexOfHyphen == G.arbitraryIndexVal:
raise InternalError("Hyphen was never found in _parse().")
else:
G.nextIndex = indexOfHyphen+1
u = _parseUsr(G.nextIndex+1,fline) #user
i = _parseIP(fline[G.nextIndex]) #ip address
G.nextIndex += 1
#print G.nextIndex, fline
a = _parseAction(G.nextIndex, fline) #action
return (m,p,d,t,u,i,a)
def _parseOriginal(line):
"""Returns original line contents, used in scrambing of information by FLA."""
fline = line.split()
fline.append('\n') #added to indicate when line ends
#print fline
if fline[0] == "FileZilla" or fline[0] == "Initializing" or \
fline[0] == "Creating" or fline[0] == "Server" or \
fline[0] == "Closing" or fline[0] == "Listen":
G.nextIndex = 0
m = 1 #line is a message
a = _parseAction(G.nextIndex,fline) #action
return (m,-1,'---','---','---','---','---',a)
else:
G.nextIndex = 0
m = 0 #line is not a message i.e. contains data to parse
p = fline[0] #port number
d = fline[1] #date
t = fline[2] #time
if fline[3] in ('AM','PM'):
f = ' '.join((fline[3],fline[4])) #AM/PM and '-'
G.nextIndex = 5
elif fline[3] == '-':
f = fline[3]
G.nextIndex = 4
else:
raise InternalError("Unknown case in _parseOriginal().")
u = _parseUsr(G.nextIndex+1,fline) #user
i = fline[G.nextIndex] #ip address
G.nextIndex += 1
#print G.nextIndex, fline
a = _parseAction(G.nextIndex, fline) #action
#print "returning", (m,p,d,t,f,u,i,a)
return (m,p,d,t,f,u,i,a)
#PUBLIC FUNCTIONS:
def getLine(line):
return _parse(line)
def getMesg(line):
(m,p,d,t,u,i,a) = _parse(file)
return m
def getPort(line):
(m,p,d,t,u,i,a) = _parse(file)
return p
def getDate(line):
(m,p,d,t,u,i,a) = _parse(line)
return d
def getTime(line):
(m,p,d,t,u,i,a) = _parse(line)
return t
def getUser(line):
(m,p,d,t,u,i,a) = _parse(line)
return u
def getIP(line):
(m,p,d,t,u,i,a) = _parse(line)
return i
def getAction(line):
(m,p,d,t,u,i,a) = _parse(line)
return a
def getOriginalLine(line):
(m,p,d,t,f,u,i,a) = _parseOriginal(line)
return (m,p,d,t,f,u,i,a)
def stripIP(ip):
return _parseIP(ip)