3

我在具有 ADSL 连接的机器上运行 SSH。每次机器有新的 IP 地址时,我都会制作这个脚本来向我发送一封电子邮件。

我无法访问该机器。我把脚本给了一个朋友,所以我无法通过调试来找出这个脚本有什么问题。我现在正在使用大学连接,它有一个静态 IP 地址。在上面运行脚本没有意义。

所以任何关于如何改进/修复脚本的建议。有时我会收到无效的 IP 地址,或者有时 IP 地址会更改但我没有收到电子邮件。我应该为这种自动化使用另一种方法吗?

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()
4

2 回答 2

5

我建议您可能过于频繁地访问服务器并被阻止... http://forum.whatismyip.com/f14/pace-yourself-t6/

将您的第一个更改time.sleep(5)time.sleep(300).

于 2012-05-03T14:22:59.243 回答
0

感谢伟大的剧本!这肯定会起作用(使用 echoip.com 作为 urlopen 数据)

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()
于 2015-05-11T15:15:19.107 回答