Automatically update and send IP address

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
    #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()
+3
source share
2 answers

, ... http://forum.whatismyip.com/f14/pace-yourself-t6/

time.sleep(5) time.sleep(300).

+5

script! , , ( echoip.com urlopen data)

import urllib
import time
import smtplib

fromaddr = '***'  
toaddrs  = '***'    
ip = ""

username = '***'  
password = '***'  
f = False

def update():
    global ip,f
    #print "sleeping 5 seconds"
    time.sleep(20)
    while not f:
        try:
            f = urllib.urlopen("http://echoip.com")
        except IOError as e:
            print ("no internet !", e)
            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://echoip.com")
            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 as e:
            print ("no internet", e)
            time.sleep(10)
            #sendmail(ip)


print "program started"

while(1):
    update()
0

All Articles