Created
May 6, 2018 22:55
-
-
Save after-ephemera/44296f08dc85e012d1147f496292f4d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 protected]' | |
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