-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_blocklist.py
71 lines (56 loc) · 2.89 KB
/
update_blocklist.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
#/usr/bin/env python
import sys, socket, urllib, urllib2, argparse, bigsuds
def main():
parser = argparse.ArgumentParser(description='Update blocklist.de data group on F5 BIG-IP device.')
parser.add_argument('-a', '--host', type=str, help='ip address of an F5 BIG-IP device', required=True)
parser.add_argument('-u', '--username', type=str, help='user to authenticate with', required=True)
parser.add_argument('-p', '--password', type=str, help='password to authenticate with', required=True)
parser.add_argument('-g', '--devicegroup', type=str, help='device group of the HA pair', required=True)
parser.add_argument('-d', '--partition', type=str, help='partition to add the data group to', default='Common')
args = parser.parse_args()
# Leave this unless you also change the iRule
data_group = "ext_blocklist"
# Create an empty data group to add IPs to block to
address_list = [{'name':'/' + args.partition + '/' + data_group, 'members':[]}]
# Get IPs from the blocklist
iplisturl = "http://lists.blocklist.de/lists/all.txt"
data = urllib.urlencode({'ipv6':'0', 'export_type':'text', 'submit':'Export'})
req = urllib2.Request(iplisturl, data)
response = urllib2.urlopen(req)
blocklist = set(response.read().splitlines())
for ip in blocklist:
# Hacky way to test if it's a valid IP
try:
socket.inet_aton(ip)
address_list[0]['members'].append({'netmask':'255.255.255.255', 'address':ip})
except socket.error:
pass
# Make sure that the list contains some stuff
if len(address_list[0]['members']) > 0:
b = bigsuds.BIGIP(hostname = args.host, username = args.username, password = args.password)
bigtrans = b.with_session_id()
bigtrans.System.Session.set_transaction_timeout(10)
try:
with bigsuds.Transaction(bigtrans):
bigtrans.Management.Partition.set_active_partition(args.partition)
# So many try's
try:
bigtrans.LocalLB.Class.modify_address_class(address_list)
except bigsuds.OperationFailed:
try:
bigtrans.LocalLB.Class.add_address_class(address_list)
except bigsuds.OperationFailed:
try:
bigtrans.LocalLB.Class.delete_class(data_group)
bigtrans.LocalLB.Class.add_address_class(address_list)
except bigsuds.OperationFailed, e:
print e
return 1
# Make sure the updated config is synced between devices
bigtrans.System.ConfigSync.synchronize_to_group(args.devicegroup)
return 0
except bigsuds.OperationFailed, e:
print e
return 1
if __name__ == "__main__":
sys.exit(main())