I have SSH running on a machine with ADSL connection. I made this script to send me an email each time the machine has new a IP address.
The machine is not accessible by me. I gave the script to a friend so I cannot perform debugging to figure out what is wrong with this script. I’m using a university connection right now and it has a static ip address. There is no point running the script on it.
So any suggestions how to improve/fix the script. Sometimes I’ll receive non valid IP addresses or sometimes the IP address will change but I don’t get an email. Should I use another method for this kind of automation?
import urllib
import time
import smtplib
fromaddr = '***@gmail.com'
toaddrs = '***@gmail.com'
ip = ""
username = '****'
password = '****'
f = False
def update():
global ip,f
#print "sleeping 5 seconds"
time.sleep(5)
while not f:
try:
f = urllib.urlopen("http://automation.whatismyip.com/n09230945.asp")
except IOError, e:
print "no internet !"
time.sleep(5)
if not ip and f:
ip = f.read()
print "getting the first ip"
print ip
sendmail(ip)
print "mail sent"
else:
if f:
ip2 = f.read()
#print ip,ip2
if ip != ip2 and ip and ip2:
ip = ip2
print "new ip",ip,"sending mail"
sendmail(ip)
else:
print "ip is the same"
f = False
#print ip
def sendmail(ip):
a = False
while not a:
try:
#just to check if i have internet or not
a = urllib.urlopen("http://automation.whatismyip.com/n09230945.asp")
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, ip)
server.quit()
except IOError, e:
print "no internet"
time.sleep(5)
#sendmail(ip)
print "program started"
while(1):
update()
I’d suggest that you might be hitting the server too often and getting blocked… http://forum.whatismyip.com/f14/pace-yourself-t6/
Change your first
time.sleep(5)totime.sleep(300).