-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnsWatch.py
executable file
·78 lines (64 loc) · 2.21 KB
/
dnsWatch.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
72
73
74
75
76
77
78
#!/usr/bin/env python3
###################################
# by lukas.spitznagel@netzint.de
# Version 1.0 from 25.02.2022
# by andreas.till@netzint.de
# Version 1.1 from 18.05.2022
# needs:
# pip3 install dnspython
#
###################################
import argparse
from dns import resolver
from sys import platform
import time
import os
def sendNotification(text, title):
if platform == "linux" or platform == "linux2":
string = "notify-send '" + title + "' '" + text + "'"
if platform == "darwin":
# determine linksafe path of mac-notify-send :)
file_path = os.path.realpath(__file__)
file_path = os.path.dirname(file_path)
string = file_path+"/mac-notify-send '" + title + "' '" + text + "'"
os.system(string.encode("utf-8"))
def checkEntry(dns, address):
res = resolver.Resolver()
res.nameservers = ['1.1.1.1']
try:
hostnameRecord = res.resolve(dns)
for rdata in hostnameRecord:
print (rdata.address)
hostnameRecord = rdata.address
except Exception as e:
print (str(e))
return False
if len(address) > 0:
if hostnameRecord != address:
return False
return hostnameRecord
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dns", required = True, help = "DNS name to check")
parser.add_argument("-a", "--address", required = False, help = "Expected address for dns entry")
args = parser.parse_args()
if not args.address:
args.address = ''
remember = 0
print("Lukas DNSWatcher has started for " + args.dns + " -> " + args.address)
try:
while True:
check = checkEntry(args.dns, args.address)
if check:
sendNotification("Entry is now set correctly!\n" + args.dns + " -> " + check, "Lukas DNS Watcher")
print("Entry is now set correctly!\n" + args.dns + " -> " + check , "Lukas DNS Watcher")
remember += 1
if remember > 5:
exit(0)
else:
print ('Entry not found yet')
time.sleep(60)
except KeyboardInterrupt:
print("Bye!")
if __name__ == "__main__":
main()