IPnotifier.py

In an effort to maintain connectivity with my LAN while traveling, I’ve written a python script that, when run, will notify myself of any changes to the Public IP address of the localhost. It notifies me in two ways: 1.) An email will be sent to myself for a more immediate notification; 2.) A Google Sheet will be updated to reflect the changed Public IP.

The Google Sheet serves more than just a means of passively reviewing my Public IP, however. It also populates other cells in the spreadsheet, which I embed into my private dashboard online, so that I can better access the services I run at home.

This is definitely a work in progress, and I’m open to any suggestions on how to improve the functionality and security.

"""
IPnotifier.py

new: This program will check my Pubic IP address. It writes the Public IP address to a Google Sheet. If this program finds a Public IP address that's different from the value in the Google Sheet, it will send me an email notifying me of the change and update the Spreadsheet accordingly. I have this set to run every hour via Windows Task Scheduler.

old: This program will monitor my Public IP. When it notices that my Public IP has changed, it will send myself an email to let me know of the updated IP address. This will help keep me informed should my ISP issue me a new IP address over time, allowing me to re-establish communications with my home network while remote.
"""
# Import gspread for interfacing with Google Sheets.
import gspread

from oauth2client.service_account import ServiceAccountCredentials

# Import socket for gethostname() functionality.
import socket

# Import smtplib for the actual sending function
import smtplib

import json

# Import the email modules we'll need
from email.mime.text import MIMEText

from urllib2 import urlopen

scopes = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']

###################################
# USER-ENTERED DATA
###################################
credentials = ServiceAccountCredentials.from_json_keyfile_name ('YOUR_CREDENTIALS_FILE.json', scopes)

gc = gspread.authorize(credentials)

###################################
# USER-ENTERED DATA
# Define the worksheet
###################################
wks = gc.open("YOUR_WORKBOOK").sheet1 ### USER-ENTERED DATA!
# Define the hostname of this computer
hostname = socket.gethostname()
# Define email sender 'me'
me = hostname
###################################
# USER-ENTERED DATA
# Define email recipient 'you'
###################################
you = "YOUR EMAIL ADDRESS" ### USER-ENTERED DATA!
text_file = open("public_ip.txt", "w")

# Use the following URL to obtain my Public IP address, and define it as "my_ip".
my_ip = urlopen('https://ip.42.pl/raw').read()

####################################
# USER-ENTERED DATA
# Define the cell in the worksheet.
####################################
ip_cell = wks.acell('YOUR CELL').value # This cell contains the Public IP Address value.

"""
-- This section will compare the public IP to the recorded IP in the Google Sheet.
-- If the IPs are different, then an email will be sent notifying the user of the change.
-- Finally, we will write the public IP Address to the Google Spreadsheet to keep it current.
"""

if ip_cell != my_ip:

text_file.write("%s: \n" % hostname)
text_file.write("%s" % my_ip)
text_file.close()

# Open a plain text file for reading. For this example, the text file contains only ASCII characters.
with open("public_ip.txt", "rb") as fp:
# Create a text/plain message
msg = MIMEText(fp.read())
msg['Subject'] = 'Public IP for %s is %s' % (hostname, my_ip)
msg['From'] = me
msg['To'] = you
msg['Body'] = 'The Public IP for the computer %s is %s' % (hostname, my_ip)

#############################
# USER-ENTERED DATA
# This is where we keep our email login credentials.
#############################
gmail_user = 'YOUR GMAIL USER'
gmail_password = 'YOUR GMAIL PASS'

#############################
# USER-ENTERED DATA
# Send via our SMTP server, without envelope header.
#############################
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()

# Upgrade to a secure TLS connection.
s.starttls()
s.login(gmail_user, gmail_password)
s.sendmail(me, [you], msg.as_string())
s.quit()

############################
# USER-ENTERED DATA
# Write localhost's public IP to the Google Sheet cell 'H1'
############################
wks.update_acell('YOUR CELL', my_ip)

Leave a Reply