Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating and sending IP address automatically
    text
    copied!<p>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. </p> <p>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. </p> <p>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?</p> <pre><code>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() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload