Skip to content

Instantly share code, notes, and snippets.

@lichray
Created August 23, 2013 21:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lichray/6324039 to your computer and use it in GitHub Desktop.
Save lichray/6324039 to your computer and use it in GitHub Desktop.
(alpha) Dynamic DNS using Rackspace Cloud DNS
#!/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