2012-05-03 3 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에 등록 (또는 유사)와 Dyndns에 클라이언트 데몬을 실행 ... – ChristopheD

+0

권리가있다 noipip.com 무료로 동일한 서비스를 제공하지만 원격 PC가 맥 OS를 실행하기 때문에 나는 파이썬 스크립트로이 작업을 할 수있는 것처럼 느낀다 – abdu

답변

0

대단한 스크립트를 보내 주셔서 감사합니다! 이것은 확실히 당신의 문제를 해결하기 위해 (urlopen 데이터로 echoip.com와)

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