My SSH works on a machine with an ADSL connection. I made this script to send me an email every time the machine has a new IP address.
The car is not available to me. I passed the script to a friend, so I cannot debug to find out what is wrong with this script. I am using a university connection right now and it has a static IP address. It makes no sense to use a script on it.
So, any suggestions for improving / fixing the script. Sometimes I get invalid IP addresses or sometimes the IP address will change, but I don’t get the email. Should I use a different 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
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()
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
def sendmail(ip):
a = False
while not a:
try:
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)
print "program started"
while(1):
update()
source
share