-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-hosts-deny.py
executable file
·71 lines (51 loc) · 1.58 KB
/
update-hosts-deny.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
# -*- coding: utf-8 -*-
#########################
# CONFIGURATION GOES HERE
# In production the filename is /etc/hosts.deny
FILENAME = '/etc/hosts.deny'
# Use this separator in file. Really no use in changing
SEPARATOR = '## AUTOGENERATED LIST'
# URL to blacklist
BLACKLIST_URL = 'http://www.openbl.org/lists/hosts.deny'
#################
# CODE GOES HERE!
import sys
import urllib2
def get_file():
"""
Read content of hosts.deny file and return code that is generated
static and dynamic. I.e. What is seperated by the string SEPARATOR.
Returns a tuple: (static_part, generated_part)
"""
with open(FILENAME, 'r') as f:
try:
content = f.read()
except Exception, e:
print str(e)
sys.exit(1)
if SEPARATOR in content:
static, dynamic = content.split(SEPARATOR)
return static.strip(), dynamic.strip()
else:
return content.strip(), ''
def update_file():
"""
Update hosts.deny-file with dynamic content
This is the main function to run.
"""
static, _ = get_file()
try:
dynamic = urllib2.urlopen(BLACKLIST_URL).read()
except Exception, e:
print str(e)
print '\nCould not fetch url:', BLACKLIST_URL
sys.exit(1)
with open(FILENAME, 'w') as f:
f.write(static + '\n\n')
f.write(SEPARATOR + '\n\n')
f.write(dynamic + '\n')
added_lines = dynamic.count('\n')
print 'Number of lines added to %s: %s' % (FILENAME, added_lines)
if __name__ == '__main__':
update_file()