Skip to content

Instantly share code, notes, and snippets.

@UnixSage
Last active July 5, 2020 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UnixSage/0aa4e4f13dc62b14e0609585cdac67ac to your computer and use it in GitHub Desktop.
Save UnixSage/0aa4e4f13dc62b14e0609585cdac67ac to your computer and use it in GitHub Desktop.
Simple script that will monitor a list of names and make sure the IPs match. Useful if you not using CNAMES for APEX domains. Output can be put on a web server and monitored for status.
#!/usr/bin/env python3
import socket
import datetime
domainlist = {
'home.mydomain.com': 'user.myisp.net',
'mydomain.com': 'account.myhosting.com'
}
statusdetail = []
serviceOK = True
for domain in domainlist:
siteip = socket.gethostbyname_ex(domain)[2]
refip = socket.gethostbyname_ex(domainlist[domain])[2]
siteip.sort()
refip.sort()
siteip = ' '.join(siteip)
refip = ' '.join(refip)
if siteip == refip:
statusdetail.append(f"""<div id="status" class="ok">{domain} ({siteip}): OK</div><br>""")
else:
statusdetail.append(f"""<div id="status" class="error">{domain} ({siteip}): ERROR new IP - {refip}</div><br>""")
serviceOK = False
report="""
<html>
<head>
<title>Monitor IP</title>
<style>
#status.ok {{color: green;}}
#status.error {{color: red;}}
</style>
</head>
<h1>Service Status: {status}</h1>
<hr>
{detail}
<div>Updated: {datestamp}</div>
</html>
"""
print(report.format
(
status = serviceOK,
detail = "\n".join(statusdetail),
datestamp = datetime.datetime.now().replace(microsecond=0)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment