Tuesday, January 15, 2013

Setting up daily public IP

Last night I installed Ubuntu on my old laptop, yet again.  In the last 6 months I have installed Linux twice and windows once.  Since I got an iPad for Christmas I wanted to try out ssh through my iPad to a Linux machine (python programming remotely).  One of the issues that I knew I would face sometime was my home ip address was not static.  What would happen if I was out at at a coffee shop and want to finish off some Project Euler problems?  Only to find out my public ip address at home has changed. 

I could pay to have a static ip address but I am not really doing that much work from outside my network.  This is more of just a hobby.  The solution I came up with was to email my public ip address every morning.  Here is how I did it.

There was 2 major problems that I would face here.  The first is getting my public ip address.  I have searched before and had seen all the wget and curl commands with whatismyip.org and the like.  This never really worked for me.  Yes I could get the html page but the ip address would be enbedded in the javascript someplace and I really did not want to spend too much time on it.  Then I came across this snippet of code

curl ifconfig.me


That simple commandline magic will return my public ip address.  It really is that simple.

The second problem was I needed to find a way to email this ip address to myself.  I found a solution from Justin Duke.  He wrote a blog post (here) about using Gmail to send email through python.  Perfect solution as I am working on learning Python as a second language.  Here is the code I am using to send myself the current public ip address at home.

import smtplib
import sys

# The below code never changes, though obviously those variables need values.
session = smtplib.SMTP('smtp.gmail.com', 587)
session.ehlo()
session.starttls()
session.login(GMAIL_USERNAME, GMAIL_PASSWORD)

headers = "\r\n".join(["from: " + GMAIL_USERNAME,
    "subject: Public IP Address",
    "to: " + recipient,
    "mime-version: 1.0",
    "content-type: text/html"])

# body_of_email can be plaintext or html!
body_of_email = sys.argv[1]
content = headers + "\r\n\r\n" + body_of_email
session.sendmail(GMAIL_USERNAME, recipient , content)


Again BIG thanks for Justin Duke for providing the Gmail code.  This is simply using the second argument as the body of the email.  When I run this script I use the newly found ip address as the second argument.

The last bit to finish this project was to add an entry in the crontab to run this once a day at 8am. Easy enough, just crontab -e and add the following.

0 8 * * * /usr/bin/python /home/jason/python/dailyIP.py $(curl ifconfig.me) >/dev/null 2>&1


This again is not a complete solution.  If my ip address gets changed in the middle of the day then I am hosed.  This will at least keep me in the loop if it happens on a day when I am not connecting in from the outside.

In case you were wondering, using iSSH I spent a good hour in bed programming on my iPad (well technically it was on the laptop).  With the Bluetooth keyboard I have for the iPad it worked perfectly.  I am pretty excited to be able to do this and it makes me love this iPad just that much more. 

**UPDATE** I changed the crontab entry to send the email twice a day.  Just found out I could do that
Code:
0 8,12 * * * /usr/bin/python /home/jason/python/dailyIP.py $(curl ifconfig.me) >/dev/null 2>&1

No comments:

Post a Comment