-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_hostsfile
165 lines (143 loc) · 4.75 KB
/
check_hostsfile
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
import sys
import os
import argparse
import traceback
import datetime
import time
import ipaddress
import dns.query
import dns.resolver
import dns.exception
#-------------------------------------------------------------------------------
_candebug = False
def candebug():
global _candebug
return _candebug
def setcandebug(value):
global _candebug
_candebug = value
def infomsg(msg):
if candebug() == True:
print(msg, flush=True)
def exitnagios(status,message):
if status=="OK":
exitcode = 0
elif status=="WARNING":
exitcode = 1
elif status=="CRITICAL":
exitcode = 2
elif status=="UNKNOWN":
exitcode = 3
else:
exitcode = 4
print(status+": "+message, flush=True)
sys.exit(exitcode)
#-------------------------------------------------------------------------------
def extractsearchlist():
result = ""
handle = None
try:
handle = open("/etc/resolv.conf","r")
lines = handle.read().splitlines()
for line in lines:
line = line.strip().split("#",1)[0]
if len(line)>0:
parts = line.split()
if parts[0]=="search":
result = parts[1].split()
break
finally:
if handle!=None:
handle.close()
return result
def extracthostslist():
result = {}
handle = None
try:
handle = open("/etc/hosts","r")
lines = handle.read().splitlines()
for line in lines:
line = line.strip().split("#",1)[0]
if len(line)>0:
parts = line.split()
key = parts[0]
result[key] = parts[1:]
finally:
if handle!=None:
handle.close()
return result
def testvalue(ipo,dnsquery,recordtype,customresolver):
result = True
try:
queryrr = customresolver.resolve(dnsquery,recordtype,search=False)
infomsg(queryrr.rrset)
if len(queryrr.rrset)==1:
resolvedip = ipaddress.ip_address(queryrr.rrset[0])
if resolvedip != ipo:
result = False
else:
result = False
except:
infomsg("Error resolving "+dnsquery+" "+recordtype)
result = False
return result
def testentry(ipo,canonical,aliases,searchlist,customresolver):
result = True
recordtype = None
if ipo.version == 4:
recordtype = "A"
elif ipo.version == 6:
recordtype = "AAAA"
if testvalue(ipo,canonical,recordtype,customresolver):
infomsg("Test value True for "+str(ipo))
for alias in aliases:
if testvalue(ipo,alias,recordtype,customresolver)==False:
foundusingsearch = False
for search in searchlist:
if testvalue(ipo,alias+"."+search,recordtype,customresolver):
foundusingsearch = True
break
infomsg(alias+" "+str(foundusingsearch))
if foundusingsearch == False:
result = False
break
else:
infomsg("Test value False for "+str(ipo))
result = False
return result
def dohostsfilecheck():
searchlist = extractsearchlist()
hosts = extracthostslist()
infomsg(searchlist)
infomsg(hosts)
allvalid = True
failing = {}
customresolver = dns.resolver.Resolver("/etc/resolv.conf")
for key,value in hosts.items():
ipo = ipaddress.ip_address(key)
if (ipo.is_loopback==False) and (ipo.is_private or ipo.is_global) and (ipo.is_reserved==False) and (ipo.is_link_local==False) and (ipo.is_multicast==False):
infomsg("Evaluating for "+str(ipo))
canonical = value[0]
aliases = value[1:]
valid = testentry(ipo,canonical,aliases,searchlist,customresolver)
allvalid = allvalid and valid
if not valid:
failing[key] = value
if allvalid:
exitnagios("OK","the hosts file seems valid")
else:
exitnagios("WARNING","some lines do not fully conform to specification or do not resolve properly "+str(failing))
#-------------------------------------------------------------------------------
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-®", "--debug", action="store_true", dest="debug", default=False, help="be more verbose")
args = parser.parse_args()
return args
def main():
args = parse_args()
setcandebug(args.debug)
dohostsfilecheck()
main()
#-------------------------------------------------------------------------------