-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathipmanage
More file actions
executable file
·182 lines (136 loc) · 3.84 KB
/
Copy pathipmanage
File metadata and controls
executable file
·182 lines (136 loc) · 3.84 KB
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
#!/usr/bin/python3
import sys
import os
ipList = {}
class Menu:
def __init__(self):
self.menuList = []
def AddItem(self, key, display):
if len(key) != 1:
return 1
self.menuList.append( (key,display) )
return 0
def AddSeparator(self):
self.menuList.append(("SEP","SEP"))
def Show(self):
for (key, display) in self.menuList:
if key == "SEP":
print()
else:
print("{} - {}".format(key, display))
print()
return input("Make a selection. >").lower()
def addToList(IP, date, time):
global ipList
existing = 0
if IP in ipList:
(count,first,last) = ipList[IP]
count += 1
else:
count = 1
first = (date, time)
last = (date, time)
ipList[IP] = (count,first,last)
def printList():
for i,(IP,(count,first,last)) in enumerate(list(ipList.items())):
print("{} - {}: {}".format(i, IP, count),end='')
print("\tFirst: {} {}".format(first[0], first[1]),end='')
print("\tLast: {} {}".format(last[0], last[1]),end='')
print()
def addListToMenu(menu, start, end):
for i,(IP,(count,first,last)) in enumerate(list(ipList.items())[start:end]):
display = "{}: {}".format(IP, count)
display += "\tFirst: {} {}".format(first[0], first[1])
display += "\tLast: {} {}".format(last[0],last[1])
menu.AddItem(str(i), display)
def mainMenu():
selectedOption = ""
page = 0
ipCount = len(ipList)
while selectedOption != "q":
theMenu = Menu()
addListToMenu(theMenu, page*10, (page*10)+10)
theMenu.AddSeparator()
if ( (ipCount > 10) and ((page * 10) + 10 < ipCount) ):
theMenu.AddItem("n","Next page")
if (page > 0):
theMenu.AddItem("p", "Previous page")
theMenu.AddItem("q", "Quit")
selectedOption = theMenu.Show()
if ((selectedOption == "p") and (page > 0)):
page -= 1
elif ((selectedOption == "n") and ((page * 10) + 10 < ipCount) ):
page += 1
elif (selectedOption == "q"):
break
else:
try:
selectedNum = int(selectedOption)
except:
print("Invalid selection.")
continue
selectedNum = (page * 10) + selectedNum
if not (selectedNum >= ipCount):
selectedIP = list(ipList.keys())[selectedNum]
selectedItemMenu(selectedIP)
else:
print("Invalid selection.")
def selectedItemMenu(IP):
selectedOption = ""
theMenu = Menu()
theMenu.AddItem("1", "WHOIS Query")
theMenu.AddItem("2", "AbuseAt CBL Query(Firefox)")
theMenu.AddSeparator()
theMenu.AddItem("b", "Back to Main Menu")
while selectedOption != "b":
(count,(firstDate,firstTime),(lastDate,lastTime)) = ipList[IP]
print("Selected IP: {}".format(IP))
print("\tTotal Entries: {}:".format(count))
print("\tFirst: {} {}".format(firstDate, firstTime))
print("\tLast: {} {}".format(lastDate, lastTime))
print()
selectedOption = theMenu.Show()
if(selectedOption == "1"):
os.system("whois {} | less".format(IP))
elif(selectedOption == "2"):
os.system("firefox http://www.abuseat.org/lookup.cgi?ip={} &".format(IP))
else:
print("Invalid selection.")
print()
def main():
fileName = "/var/log/iptables.log"
testMode = False
interactiveMode = False
verboseMode = False
if(len(sys.argv) > 1):
for arg in sys.argv:
if arg == "-t":
fileName = "./testdata"
testMode = True;
interactiveMode = True if arg == "-i" else (False)
if arg == "-p":
if testMode:
print("Error: -t and -p are mutually exclusive.")
sys.exit(1)
fileName = fileName + ".1"
verboseMode = True if arg == "-v" else (False)
lineCount = 0
with open(fileName, "r") as f:
for line in f:
srcPos = line.find("SRC=")
if srcPos != -1:
srcEnd = line.find(' ', srcPos)
toShow = line[srcPos + 4 : srcEnd]
date = line[:6]
time = line[7:15]
addToList(toShow, date, time)
lineCount += 1
print("\n{} access attempts.\n".format(lineCount))
if interactiveMode:
mainMenu()
elif verboseMode:
printList()
print()
return
if __name__ == "__main__":
main()