-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_iodine
115 lines (98 loc) · 3.68 KB
/
check_iodine
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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
import os
import sys
import subprocess
import argparse
import mechanize
import time
from bs4 import BeautifulSoup
#-------------------------------------------------------------------------------
_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 doportexternalcall(domain):
try:
valid = False
url = "https://code.kryo.se/iodine/check-it/"
retries = 3
counter = 0
while True:
counter = counter+1
welldone = False
try:
infomsg("initializing browser")
br = mechanize.Browser()
br.set_handle_robots(False) # ignore robots file
#br.addheaders = [('User-agent', 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)')]
br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0')]
br.open(url)
infomsg("page opened")
br.select_form(nr=0)
br["domain"] = str(domain)
infomsg("going to submit!")
res = br.submit()
infomsg("submitted")
content = res.read()
infomsg("the result is of length "+str(len(content)))
if content != None:
soup = BeautifulSoup(content, "html.parser")
#infomsg(soup)
div = soup.find("div", id="blaffa")
#infomsg(div)
wdbold = soup.find_all("b")
welldone = False
for item in wdbold:
if item.text == "Well done":
welldone = True
except Exception as e:
print(str(e),flush=True)
finally:
valid = False
if welldone == True:
valid = True
break
elif counter>=retries:
break
else:
time.sleep(10)
if valid == True:
exitnagios("OK","the tunnel is well done")
else:
exitnagios("WARNING","the tunnel is not valid")
except Exception as e:
exitnagios("CRITICAL","unexpected error during the check "+str(e))
#-------------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-H", "--hostname", dest="hostname", default="", help="server to check")
parser.add_argument("-®", "--debug", action="store_true", dest="debug", default=False, help="be more verbose")
args = parser.parse_args()
setcandebug(args.debug)
if (args.hostname == ""):
exitnagios("CRITICAL","hostname not defined")
doportexternalcall(args.hostname)
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------