Created
August 23, 2013 21:08
-
-
Save lichray/6324039 to your computer and use it in GitHub Desktop.
(alpha) Dynamic DNS using Rackspace Cloud DNS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
INTERVAL = 300 | |
import clouddns | |
import requests | |
try: | |
from gi.repository import GObject as gobject | |
except ImportError: | |
import gobject | |
def get_outgoing_ip(): | |
result = requests.get('http://icanhazip.com/') | |
return result.content.strip() | |
def Updater(cfg): | |
dns = clouddns.connection.Connection(username=cfg['username'], | |
api_key=cfg['api_key']) | |
domain = dns.get_domain(name=cfg['domain']) | |
record = domain.get_record(name=cfg['record']) | |
self = {'stored_ip': bytes(record.data)} | |
def sync_ip(): | |
ip = get_outgoing_ip() | |
if self['stored_ip'] != ip: | |
record.update(data=ip) | |
self['stored_ip'] = ip | |
def sync_ip_loop(): | |
sync_ip() | |
gobject.timeout_add_seconds(INTERVAL, sync_ip_loop) | |
def run(): | |
sync_ip_loop() | |
gobject.MainLoop().run() | |
return run | |
if __name__ == '__main__': | |
import os.path | |
import ConfigParser | |
parser = ConfigParser.RawConfigParser() | |
parser.read(os.path.expanduser('~/.config/update-clouddns.ini')) | |
cfg = parser.defaults() | |
updater = Updater(cfg) | |
try: | |
updater() | |
except KeyboardInterrupt: | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment