Return to Snippet

Revision: 12107
at March 2, 2009 16:25 by java2025


Initial Code
#!/usr/bin/env python

import time, urllib, logging, socket, traceback

SITE_1_URL = 'vietnam1.net'
SITE_1_PW = '134243242'
SITE_1_URL = 'vietnam2.net'
SITE_2_PW = '234236656'

UPDATE_INTERVAL = 60#1 minute
ip = socket.gethostbyname('theheviet.net')
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(levelname)s %(message)s'
                    ,filename='/var/log/padyn.log',filemode='a'
                    )

def update_dns(domain,pw):
    f = urllib.urlopen('http://support.pavietnam.vn/padyn.php?host=@|www&ip=' 
                       + ip + '&domain=' + domain + '&password=' + pw + '&ran=32834')
    ret = f.read()
    f.close()
    if ret != 'ok':
        logging.error('dns update was not successful')
    else:
        logging.info('dns update successfull: ' + domain + ' => ' + ip)

logging.info('padyn started')
while 1:
    try:
        f = urllib.urlopen('http://pavietnam.vn/getip.php')
        my_ip = f.read()
        f.close()
        logging.debug('got public IP: ' + my_ip)
        
        if my_ip != ip:
            logging.info('detect IP changed from ' + ip + ' => ' + my_ip + ', start updating dns...')
            ip = my_ip
#            update_dns(SITE_1_URL,SITE_1_PW)
            update_dns(SITE_2_URL,SITE_2_PW)
        else:
            logging.debug('IP does not change')
            
            time.sleep(UPDATE_INTERVAL)
    except Exception:
        logging.error(traceback.format_exc())

Initial URL

                                

Initial Description

                                

Initial Title
IP update client for pavietnam

Initial Tags

                                

Initial Language
Python