いつもお世話になっているieServerでDDNSを更新するためのスクリプトをPythonで書いた。
しかし、perlとwgetを使うものが用意されているので、Pythonのものをわざわざ使う意味はあまりない。
http://ieserver.net/ddns-update.txt
自己満足ということで。
#!/usr/bin/python import os import urllib2 import datetime IP_IGNORE = '0.0.0.0' CURRENT_IP_FILE = 'current_ip' LOG_FILE = 'update.log' REMOTE_ADDR_CHK = 'http://ieserver.net/ipcheck.shtml' DDNS_UPDATE = 'https://ieserver.net/cgi-bin/dip.cgi' ACCOUNT = '' DOMAIN = '' PASSWORD = '' def main(): if os.path.exists(CURRENT_IP_FILE): f = open(CURRENT_IP_FILE, 'r') current_ip = f.read() f.close() else: current_ip = IP_IGNORE print 'Current IP: %s' % current_ip try: port = urllib2.urlopen(REMOTE_ADDR_CHK) new_ip = port.read() port.close() except: new_ip = IP_IGNORE print 'New IP: %s' % new_ip if (new_ip != IP_IGNORE) and (current_ip != new_ip): try: port = urllib2.urlopen(DDNS_UPDATE + '?username=%s&domain=%s&password=%s&updatehost=1' % (ACCOUNT, DOMAIN, PASSWORD)) status = port.read() port.close() import re r = re.compile(new_ip) if r.search(status): updated = True else: updated = False except: updated = False logfile = open(LOG_FILE, 'a') if updated: logfile.write('%s %s.%s Update %s to %s\n' % (datetime.datetime.now(), ACCOUNT, DOMAIN, current_ip, new_ip)) ipfile = open(CURRENT_IP_FILE, 'w') ipfile.write(new_ip) ipfile.close() else: logfile.write('%s %s.%s Update aborted %s to %s\n' % (datetime.datetime.now(), ACCOUNT, DOMAIN, current_ip, new_ip)) logfile.close() if __name__ == '__main__': main()