Non fixed IP address headache

Sunday, 21 December 2008 14:44
Bit of a random tutorial this one. I dont know how most people operate, but I like to geek it up every now and again and need to access my computer externally. Doing this I can get access to my files, or control my download manager, or get back at that password that I simply just cant remember. Normally this wouldnt be a problem, except my computer doesnt have a fixed IP address.

So unless I've taken the time in the morning to check my external IP address, and mail it to myself, then I never have a clue what it is. Especially as one of my house mates striaghteners uses the same plus socket as the router, so it often gets randomly unplugged, and a new IP address assigned.

Nightmare situations eh?

But not all is at loss. Thanks to the wonderful tool that is python, I can now add a little scheduled task (in the windows control panel) that runs this script I made.

import urllib2

# Get external IP
html = urllib2.urlopen("http://myip.dk").read(206)
end = html.find("")
start = html.find("Your IP address is:") + 19

current_ip = html[start:end].strip()
#print ip

# Get old external IP
fileObj = open("c:\current_ip.txt","r")
old_ip = fileObj.read()
#print old_ip
fileObj.close()

# Check to see if its changed
if current_ip!=old_ip:
print "IP has changed, sending email notification"
# Save new current one for next check
fileObj = open("c:\current_ip.txt","w")
fileObj.write(current_ip)
fileObj.close()

# Email New IP
import sys, smtplib
fromaddr = " This e-mail address is being protected from spambots. You need JavaScript enabled to view it "
toaddrs = " This e-mail address is being protected from spambots. You need JavaScript enabled to view it "
msg = "Your ip has changed. Its new value is: " + current_ip

# The actual mail send
server = smtplib.SMTP('localhost') #Change this to an smtp server of your choice
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

What this does is to check every hour (or however often u set it) that your ip address is the same as the last time it checked it. If any change has occured, that it makes it it's job to simply email you with the new ip address :)
Last Updated ( Sunday, 21 December 2008 14:56 )
 
Sitemap