Skip to content

Instantly share code, notes, and snippets.

@UnixSage

UnixSage/MonitorIP.py

Last active Jul 5, 2020
Embed
What would you like to do?
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