Skip to content

Instantly share code, notes, and snippets.

@matteoferla
Last active August 30, 2021 05:41
Show Gist options
  • Save matteoferla/e52ff69d6787f4fb6898b62275ab7773 to your computer and use it in GitHub Desktop.
Save matteoferla/e52ff69d6787f4fb6898b62275ab7773 to your computer and use it in GitHub Desktop.
A script to be run as, say, an hourly cron job to check wheather the IP address of the machine has changed.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__description__ = \
"""
A script to check if the IP address is running.
Use it add your settings to the fields below.
Once you are sure you have all the right keys and it works, install an hour cron job thusly:
$sudo nano crontab -e
0 * * * * python3 ~/Coding/IP_check/IP_check.py >> ~/Coding/IP_check/log.txt
where IP_check/IP_check.py is wherever you want to put the script.
The script requires dig: sudo apt-get install dnsutils
And the python library godaddypy
Remember to change the slave gmail address's security to be used by third parties (ie. your script).
Remember you need to use a production key for Godaddy to work.
"""
__author__ = "Matteo Ferla. [Github](https://github.com/matteoferla)"
__email__ = "matteo.ferla@gmail.com"
__date__ = ""
__license__ = "Cite me!"
__version__ = "2.0"
####### change these.
class usersettings():
api_key='qwertyuiopasdfghjklzxcvbnm'
api_secret = 'qwertyuiopasdfghjklzxcvbnm'
gmail_pass = 'password'
gmail_user = 'your.slave.email.for.spam@gmail.com'
email_recipient = 'your.real.eamil@email.com'
domain = 'yourdomain.com'
subdomain_list=['sub1', 'sub2', 'sub3']
##########
import sys
assert sys.version_info[0] == 3, 'Sorry untested in python 2'
import godaddypy, os, smtplib, traceback
acct = godaddypy.Account(api_key=usersettings.api_key,
api_secret=usersettings.api_secret)
client = godaddypy.Client(acct)
def send_email(body):
subject = 'IP address change'
recipient = usersettings.email_recipient
addressee = recipient if type(recipient) is list else [recipient]
# Prepare actual message
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (usersettings.gmail_user, ", ".join(addressee), subject, body)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(usersettings.gmail_user, usersettings.gmail_pass)
server.sendmail(usersettings.gmail_user, addressee, message)
server.close()
return 0
def change_ip(ip, subdomain):
return client.update_record_ip(ip=ip, domain=usersettings.domain, name=subdomain, record_type='A')
try:
file='myIP.txt'
if os.path.isfile(file):
known_ip=open(file,'r').read()
else:
known_ip='0.0.0.0'
send_email('Could not find the file myIP.txt')
current_ip=os.popen('dig TXT +short o-o.myaddr.l.google.com @ns1.google.com').read()
if current_ip != known_ip:
success=all([change_ip(current_ip, sd) for sd in usersettings.subdomain_list])
if success:
note = 'No action required. '
else:
note='ERROR!'
note = 'IP has changed from {0} to {1}\n'.format(known_ip, current_ip)
note+=str(client.get_records(domain=usersettings.domain, record_type='A'))
send_email(note)
open(file,'w').write(current_ip)
print(note)
else:
print('No action required.')
except Exception:
err=str(traceback.format_exc())
print(err)
send_email(err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment