Last active
June 8, 2016 15:36
-
-
Save lasley/3857dfd338cda4860cb2 to your computer and use it in GitHub Desktop.
Dynamic DNS With CloudFlare
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 | |
# -*- 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