#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']
#!/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()