-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfail2ufw.py
35 lines (29 loc) · 930 Bytes
/
fail2ufw.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
#!/usr/bin/python3
import subprocess
import sys
# block ranges in the firewall
def block (what):
cmd2 = 'sudo ufw insert 1 deny from ' + what + ' to any'
print('Blocking: ' + what)
p2 = subprocess.Popen(cmd2, shell=True)
p2.wait()
print('---')
# read command line args
if (len(sys.argv) != 3):
print('Usage: fail2ufw.py <fail2ban jail name> <number of failed attempts>')
sys.exit(0)
else:
jail = str(sys.argv[1])
badNumber = int(sys.argv[2])
# execute the check
cmd = 'sudo fail2ban-client status ' + jail + ' | grep Banned | cut -d ":" -f 2 | tr " " "\n" | tr -d "[:blank:]" | cut -d "." -f 1,2 | sort | uniq -c | sort -r -n | tr -s " "'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
ips = p.stdout.readlines()
for line in ips:
line = line.strip()
ingrd = line.split()
count = int(ingrd[0])
range = str(ingrd[1])
if (count >= badNumber):
block(range[2:-1] + '.0.0/16')
sys.stdout.flush()