My old ISP use to change my external IP address all the time and to keep my DNS name on track I wrote this script:
#!/usr/bin/python
import smtplib
import urllib2
import time
import sys
def send_email():
try:
server = smtplib.SMTP('smtp', 587)#smtp address and port number
server.starttls()
#Next, log in to the server
server.login("smtp username", "password")#username and password for smtp
#def emailSend(ipaddress):
to_addr="your email address"
subject="IPaddress is changed"
message="this is from your webserver:" + html
from_addr="you@gmail.com"
header = 'From: %s\n' % from_addr
header += 'To: %s\n' % to_addr
header += 'Subject: %s\n\n' % subject
message = header + message
#Send the mail
#msg = "\nHello!" # The /n separates the message from the headers
time.sleep(10)
print "wait few seconds..."
server.sendmail(from_addr, to_addr, message)
#server.sendmail(from_addr, to_addr, message)
print "done."
return True
except:
print "Unexpected error:", sys.exc_info()[0]
return False
response = urllib2.urlopen('http://myip.dnsdynamic.org')
html = response.read()
print html
f = open("/root/myip.txt", "r");
sss = f.read()
print f.read()
if sss != html:
print 'write'
ff = open("/root/myip.txt", "w");
ff.write(html)
ff.close()
if send_email() == False:
send_email()
else:
print 'all good'
f.close()
I hope it will be useful to some one else!
Enjoy.