2012-05-03 26 views
3

私はSSHをADSL接続のマシン上で実行しています。マシンが新しい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

おそらく、より堅牢(echoip.com urlopenデータなどで)動作します:DynDNSの(または類似の)アカウントを登録し、ダイナミックDNSクライアントデーモンを実行... – ChristopheD

+0

あなたの権利を、そこにありますno-ip.comは無料で同じサービスを提供していますが、私はリモートPCがMac OS Xを実行しているので、Pythonスクリプトでこの作業をすることができるように感じています – abdu

答えて

5

私はあなたがあまりにも頻繁にサーバーを打つかもしれないし、ブロック取得することをお勧めしたい...

http://forum.whatismyip.com/f14/pace-yourself-t6/はあなたの最初の time.sleep(5) time.sleep(300)に変更し

0

偉大なスクリプトありがとう!これは間違いなくあなたの問題を解決するための

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

o男、このコードは古いです。 – abdu

関連する問題