Created
July 10, 2012 21:26
-
-
Save espennilsen/3086339 to your computer and use it in GitHub Desktop.
Lynode dyndns
This file contains hidden or 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/python | |
| # Author: Espen A. Nilsen | |
| # Requires: https://github.com/tjfontaine/linode-python | |
| # Note: This is a lazy script, you have to create the A record you want to update first. | |
| # Edit the variables: | |
| APIKEY = 'secretapikey' # Get your key from https://manager.linode.com/profile/index | |
| DOMAIN = 'mydomain.com' | |
| HOSTNAME = 'home' # Exempli Gratia: home.mydomain.com | |
| # Leave the rest to the professionals. | |
| import logging | |
| import datetime | |
| import pycurl | |
| import cStringIO | |
| from linode import api | |
| logging.basicConfig(level=logging.INFO, | |
| format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', | |
| datefmt='%m-%d %H:%M', | |
| filename='logs/dns.log') | |
| # define a Handler which writes INFO messages or higher to the sys.stderr | |
| console = logging.StreamHandler() | |
| console.setLevel(logging.ERROR) | |
| # set a format which is simpler for console use | |
| formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') | |
| # tell the handler to use this format | |
| console.setFormatter(formatter) | |
| # add the handler to the root logger | |
| logging.getLogger('').addHandler(console) | |
| def getdomainid(domain): | |
| logging.debug('Getting domainid...') | |
| linode = api.Api(APIKEY) | |
| DOMAIN_ID = linode.domain_list(name=domain)[0]['DOMAINID'] | |
| logging.debug('Got domainid %s' % DOMAIN_ID) | |
| return DOMAIN_ID | |
| def getresourceid(domainid, hostname): | |
| logging.debug('Getting resourceid...') | |
| linode = api.Api(APIKEY) | |
| resources = linode.domain_resource_list(domainid=domainid) | |
| for r in resources: | |
| if r['NAME'] == hostname: | |
| logging.debug('Got resourceid %s' % r['RESOURCEID']) | |
| return r['RESOURCEID'] | |
| def main(domain=DOMAIN, hostname=HOSTNAME): | |
| logging.debug('Starting update task') | |
| linode = api.Api(APIKEY) | |
| DOMAIN_ID = getdomainid(domain) | |
| RESOURCE_ID = getresourceid(DOMAIN_ID, hostname) | |
| linode.domain_resource_update(domainid=DOMAIN_ID, resourceid=RESOURCE_ID, target='[remote_addr]') | |
| logging.info('IP update complete') | |
| return "IP update complete" | |
| if __name__ == "__main__": | |
| exit(main()) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed the filemode on the logger.