python dh_dyn_dns.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python dh_dyn_dns.py相关的知识,希望对你有一定的参考价值。

#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()

以上是关于python dh_dyn_dns.py的主要内容,如果未能解决你的问题,请参考以下文章

代写python,代写python编程,python代写,python编程代写,留学生python代写

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍