-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_forbid_ip.py
More file actions
43 lines (40 loc) · 876 Bytes
/
add_forbid_ip.py
File metadata and controls
43 lines (40 loc) · 876 Bytes
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
import sys
import os
import commands
drop_ips = { }
rdline = ""
for rdline in sys.stdin:
drop_ip = rdline.split("\n")[0].strip(" ")
str=os.popen("iptables -L -n").read()
lines=str.split("\n")
# load dropped ips from output of iptables
for line in lines:
line=line.split(" ")
if len(line) <= 1:
continue
if line[0] != "DROP":
continue
drop_line = [""]*20
i = 0
for d in line:
if len(d) > 0:
drop_line[i] = d
i = i + 1
else:
pass
if drop_line[0] == "DROP":
if drop_line[3] not in drop_ips:
drop_ips[drop_line[3]] = 1
else:
pass
else:
pass
#for ip in drop_ips:
#cmd = "iptables -D INPUT -s " + ip + " -j DROP"
#print "cmd=%s" %cmd
#os.popen(cmd)
# add the new ip being dropped to iptables
if drop_ip not in drop_ips:
cmd="iptables -I INPUT -s " + drop_ip + " -j DROP"
os.popen(cmd)
print "%s" %cmd