Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DDNS With LibCloud (Python)
#!/usr/bin/env python
##
# DNS Updater
#
# Manipulates DNS using APIs, sets to current IP if different
# @replaces dyndns.org
#
# @author David Lasley <dave@dlasley.net>
# @package toolbox
# @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
# @url https://blog.dlasley.net/2013/08/ddns-using-libcloud-edgerouter/
import urllib2
from libcloud.dns.types import Provider
from libcloud.dns.providers import get_driver
#
# Edit Here
#
USERNAME = 'ENTER_YOUR_USERNAME'
API_KEY = 'ENTER_YOUR_API_KEY'
cls = get_driver(Provider.RACKSPACE_US) #< Provider driver, https://ci.apache.org/projects/libcloud/docs/supported_providers.html#id98
dynamic_zones = { #< These zones will be edited - {zone_domain_name:[alter_records]}
'dlasley.net' : [ #< Zone domain name
'vpn', 'server', 'puppet', 'test' #< List of record names to alter
]
}
#
# Stop Editing
#
IP = urllib2.urlopen('http://ifconfig.me/ip').read().strip()
driver = cls(USERNAME, API_KEY)
edit_record_type = driver._string_to_record_type('A')
for zone in driver.list_zones():
try:
active_zone = dynamic_zones[zone.domain]
for record in driver.list_records(zone):
if record.name in active_zone:
if record.data != IP:
if record.type == edit_record_type:
driver.update_record(record, record.name, record.type,
IP, record.extra)
print '%s.%s Updated From %s to %s' % (
zone.domain, record.name, record.data, IP
)
else:
pass
except KeyError:
pass
print 'Success!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment