Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib
- import smtplib
- # Emails new ip address when it changes via gmail
- # This is designed to be used with an OS scheduler
- # The url is not to be used more often than ever 300 seconds
- def get_ip():
- # Queries url to receive external ip address
- ipurl = 'http://automation.whatismyip.com/n09230945.asp'
- return urllib.urlopen(ipurl).readlines()[0]
- def send_new_ip(ip):
- msg = "New ip address is " + ip
- # From and to email addresses
- fromaddr = '[email protected]'
- toaddr = '[email protected]'
- #gmail credentials
- username = 'username'
- password = 'password'
- #send email
- server = smtplib.SMTP('smtp.gmail.com:587')
- server.starttls()
- server.login(username,password)
- server.sendmail(fromaddr, toaddr, msg)
- server.quit()
- print "Email sent to " + toaddr
- #writes ip to <filename>
- def write_ip(ip, filename):
- FILE = open(filename,'w')
- FILE.write(ip + '\n')
- FILE.close()
- ip = get_ip()
- filename = 'ipaddress.txt'
- try:
- FILE = open(filename,'r')
- old_ip = FILE.read().strip()
- FILE.close()
- # if new ip, sends email and replaces <filename>
- if ip != old_ip:
- send_new_ip(ip)
- write_ip(ip, filename)
- else:
- exit
- # if <filename> does not exist, creates it and sends email
- except:
- write_ip(ip, filename)
- send_new_ip(ip)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement