Skip to content

Instantly share code, notes, and snippets.

@lasley
Last active Jun 8, 2016
Embed
What would you like to do?
Dynamic DNS With CloudFlare
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
# DNS Updater
#
# Manipulates DNS using APIs, sets to current IP if different
# @replaces dyndns.org
#
# @author David Lasley <dave@laslabs.com>
# @package toolbox
#
# Edit Start Here
#
USERNAME = 'EMAIL'
API_KEY = 'API_KEY'
dynamic_zones = { #< These zones will be edited - {zone_domain_name:[alter_records]}
'dlasley.net' : [ #< Zone domain name
'test', 'test2' #< List of record names to alter
]
}
#
# Do not edit below this line
#
import urllib2
import sys
sys.path.append('./')
try:
from .cloudflare import CloudFlare
except ImportError:
print 'cloudflare.py module not present in working directory.'
print 'Download from https://gist.github.com/lasley/dc19fc32f3377fe672bc5688d4a80382'
exit()
IP = urllib2.urlopen('http://ifconfig.me/ip').read().strip()
flare = CloudFlare(API_KEY, USERNAME)
zone_map = {'zone':'zone_name', 'type_':'type', 'name':'name', 'ttl':'ttl',
'prio':'prio', 'service':'service_mode', 'rec_id':'rec_id'}
for domain, records in dynamic_zones.iteritems():
found = []
try:
for record in flare.rec_load_all(domain)['objs']:
if record['display_name'] in records:
edited = {'content':IP}
edited.update(dict((key, record[val]) for key, val in zone_map.iteritems()))
flare.rec_edit(**edited)
found.append(record['display_name'])
for record in records:
if record not in found:
flare.rec_edit(domain, 'A', '%s.%s' % (record, domain), IP)
except KeyError as e:
print e
print domain
print 'Success!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment