1 |
jpp |
1.1 |
#!/usr/bin/python |
2 |
|
|
# This program help sysadmin to reenable a hosts blocked by denyhosts. |
3 |
|
|
# |
4 |
|
|
# dh_reenable (c) 2008 Marco Bertorello <marco@bertorello.ns0.it> and is |
5 |
|
|
# free software. You can use, modify and redistribute it under terms of |
6 |
|
|
# GNU General Public License version 2 or later, as you whish, as published by |
7 |
|
|
# Free Software Foundation. |
8 |
|
|
# |
9 |
|
|
# You can get a full copy of license here: |
10 |
|
|
# |
11 |
|
|
# http://www.gnu.org/licenses/gpl-2.0.txt |
12 |
|
|
# |
13 |
|
|
# and |
14 |
|
|
# |
15 |
|
|
# http://www.gnu.org/licenses/gpl-3.0.txt |
16 |
|
|
|
17 |
|
|
|
18 |
|
|
from __future__ import with_statement |
19 |
|
|
import os |
20 |
|
|
import sys |
21 |
|
|
import fileinput |
22 |
|
|
import re |
23 |
|
|
|
24 |
|
|
# file definition: |
25 |
|
|
|
26 |
|
|
HOSTSFILE='/var/lib/denyhosts/hosts' |
27 |
|
|
HOSTRESTFILE='/var/lib/denyhosts/hosts-restricted' |
28 |
|
|
HOSTROOTFILE='/var/lib/denyhosts/hosts-root' |
29 |
|
|
HOSTVALIDFILE='/var/lib/denyhosts/hosts-valid' |
30 |
|
|
HOSTSDENY='/etc/hosts.deny' |
31 |
|
|
#TEST='/etc/hosts.deny.tmp' |
32 |
|
|
CONFIGFILE='/etc/denyhosts.conf' |
33 |
|
|
|
34 |
|
|
# Parse the configuration file for the location of the HOSTS_DENY file. |
35 |
|
|
# If it exists, overwrite the hard-coded value for HOSTSDENY from the |
36 |
|
|
# top of the file. |
37 |
|
|
if os.path.isfile(CONFIGFILE): |
38 |
|
|
with open(CONFIGFILE) as file: |
39 |
|
|
for line in file: |
40 |
|
|
result = re.search('^(HOSTS_DENY\s*=\s*)(.*)', line) |
41 |
|
|
if result != None: |
42 |
|
|
HOSTSDENY = result.group(2) |
43 |
|
|
|
44 |
|
|
def usage(): |
45 |
|
|
print "Usage:" |
46 |
|
|
print sys.argv[0]+" --help: Show this help" |
47 |
|
|
print sys.argv[0]+" <IP>: check if the ip address was denied and reenable it" |
48 |
|
|
print sys.argv[0]+" <HOSTNAME>: check if the hostname was denied and reenable it" |
49 |
|
|
|
50 |
|
|
try: |
51 |
|
|
host=sys.argv[1] |
52 |
|
|
except: |
53 |
|
|
print sys.argv[0]+" need a hostname or a ip address input. See --help." |
54 |
|
|
sys.exit(1) |
55 |
|
|
|
56 |
|
|
if host == "--help": |
57 |
|
|
usage() |
58 |
|
|
sys.exit(1) |
59 |
|
|
|
60 |
|
|
def search(file_txt,host): |
61 |
|
|
for lines in fileinput.FileInput(file_txt, inplace=1): |
62 |
|
|
lines = lines.strip() |
63 |
|
|
if lines.find(host) != -1: |
64 |
|
|
continue |
65 |
|
|
else: |
66 |
|
|
print lines |
67 |
|
|
|
68 |
|
|
|
69 |
|
|
try: |
70 |
|
|
search(HOSTSFILE,host) |
71 |
|
|
except: |
72 |
|
|
print "Problem parsing file "+HOSTSFILE |
73 |
|
|
sys.exit(1) |
74 |
|
|
try: |
75 |
|
|
search(HOSTRESTFILE,host) |
76 |
|
|
except: |
77 |
|
|
print "Problem parsing file "+HOSTRESTFILE |
78 |
|
|
sys.exit(1) |
79 |
|
|
try: |
80 |
|
|
search(HOSTROOTFILE,host) |
81 |
|
|
except: |
82 |
|
|
print "Problem parsing file "+HOSTROOTFILE |
83 |
|
|
sys.exit(1) |
84 |
|
|
try: |
85 |
|
|
search(HOSTVALIDFILE,host) |
86 |
|
|
except: |
87 |
|
|
print "Problem parsing file "+HOSTVALIDFILE |
88 |
|
|
sys.exit(1) |
89 |
|
|
try: |
90 |
|
|
search(HOSTSDENY,host) |
91 |
|
|
except: |
92 |
|
|
print "Problem parsing file "+HOSTSDENY |
93 |
|
|
sys.exit(1) |
94 |
|
|
|
95 |
|
|
print "Done!" |
96 |
|
|
print "Please restart denyhosts" |
97 |
|
|
|