Skip to content

Instantly share code, notes, and snippets.

@after-ephemera
Created May 6, 2018 22:55
Show Gist options
  • Save after-ephemera/44296f08dc85e012d1147f496292f4d2 to your computer and use it in GitHub Desktop.
Save after-ephemera/44296f08dc85e012d1147f496292f4d2 to your computer and use it in GitHub Desktop.
'''
This script is meant to be run periodically to check if this machine's
public IP address is the same or if it has changed.
'''
# Import smtplib for email functionality
import smtplib
from email.mime.text import MIMEText
from urllib.request import urlopen
from datetime import datetime
''' Closes file descriptors '''
def cleanUp:
currentIPFile.close()
logFile.close()
logFile = open('log.txt', 'a')
currentIPFile = open('currentip.txt')
currentIP = currentIPFile.read()
# Check if the IP Address has changed
myIP = urlopen('http://icanhazip.com').read().decode('utf-8')
if currentIP == myIP:
logFile.write('%s ; IP Addresses are the same. Exiting' % str(datetime.now()))
cleanUp()
exit(0)
sender = 'ipNotifier'
receiver = 'email@email.com'
msg = MIMEText('This device\'s IP Address may have changed. It is now %s' % myIP)
msg['Subject'] = 'WARNING: Potential IP Address Change'
msg['From'] = sender
msg['To'] = receiver
# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')
# s.send_message(msg)
s.sendmail(sender, [receiver], msg.as_string())
cleanUp()
s.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment