2012-05-03 84 views
3

我在運行ADSL的計算機上運行SSH。每當機器有新的IP地址時,我都會使用此腳本向我發送電子郵件。自動更新併發送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

也許更強大的解決問題的方法:註冊一個DynDNS的(或類似)帳戶和運行動態DNS客戶機守護...... – ChristopheD

+0

您的權利,有no-ip.com免費提供相同的服務,但我覺得我可以使這個工作與Python腳本,因爲遠程電腦運行mac os – abdu

回答

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() 
+0

男人,這個代碼是舊的醜陋,你應該改善它:) – abdu