Skip to content

Instantly share code, notes, and snippets.

@eristoddle
Created August 10, 2012 20:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eristoddle/3317550 to your computer and use it in GitHub Desktop.
Save eristoddle/3317550 to your computer and use it in GitHub Desktop.
Dynamic DNS Script for the Dreamhost API
#!/usr/bin/python
#Adapted from http://wiki.dreamhost.com/User:Datagrok/ddns
from dreamhostapi import DreamHostServerProxy
from os import environ
import urllib2
def main():
#Your Dreamhost API key
dh_api_key = ''
#Your hostnames to set dns for
hostnames = ['mydomain.com','*.mydomain.com']
#Working on a better way to do this
#new_ip = environ['SSH_CLIENT'].split()[0]
new_ip = pub_ip = urllib2.urlopen("http://automation.whatismyip.com/n09230945.asp").read()
server = DreamHostServerProxy(dh_api_key)
if __debug__:
print "Retrieving list of current domains..."
records = [r for r in server.dns.list_records()]
for hostname in hostnames:
for record in records:
if not (record['record'] == hostname and record['type'] == 'A'):
continue
if record['value'] == new_ip:
print "Old record for %(record)s found with IP %(value)s, no update needed." % record
continue
print "Old record for %(record)s found with IP %(value)s, removing." % record
result = server.dns.remove_record(
record=record['record'],
type=record['type'],
value=record['value'],
)
result = server.dns.add_record(
comment='Dynamic DNS IP',
record=hostname,
type='A',
value=new_ip,
)
if result != 'record_added':
raise ValueError("There was a problem adding the record.")
if __debug__:
print "Success"
if __name__=="__main__":
main()
#From http://wiki.dreamhost.com/User:Datagrok/ddns
from xmlrpclib import ServerProxy, _Method
from uuid import uuid1
class DreamHostMethod(_Method):
def __init__(self, send, name):
_Method.__init__(self, send, name)
def __getattr__(self, name):
return DreamHostMethod(self._Method__send, "%s-%s" % (self._Method__name, name))
def __call__(self, **kw):
return self._Method__send(self._Method__name, kw)
class DreamHostServerProxy(ServerProxy):
dh_api_server = 'https://api.dreamhost.com/xmlrpc'
def __init__(self, key, verbose=0):
ServerProxy.__init__(self, uri=self.dh_api_server, verbose=verbose)
self.dh_api_key = key
def __getattr__(self, name):
return DreamHostMethod(self.__request, name)
def __request(self, name, params):
if not params:
params = {}
params['key'] = self.dh_api_key
params['uuid'] = str(uuid1())
result = ServerProxy._ServerProxy__request(self, name, (params,))
if result['result'] != 'success':
raise ValueError("Server returned %(result)s: %(data)s" % result)
return result['data']
@unknownn-
Copy link

I get an error when the script attempts to remove the old IP and add the new one:

Traceback (most recent call last):
File "./ddns.py", line 49, in
main()
File "./ddns.py", line 38, in main
value=new_ip,
File "/Users/Daniel/Library/Scripts/Dreamhost/dreamhostapi.py", line 13, in call
return self._Method__send(self._Method__name, kw)
File "/Users/Daniel/Library/Scripts/Dreamhost/dreamhostapi.py", line 33, in __request
raise ValueError("Server returned %(result)s: %(data)s" % result)
ValueError: Server returned error: no_such_zone

Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment