Skip to content

Instantly share code, notes, and snippets.

@espennilsen
Created July 10, 2012 21:26
Show Gist options
  • Select an option

  • Save espennilsen/3086339 to your computer and use it in GitHub Desktop.

Select an option

Save espennilsen/3086339 to your computer and use it in GitHub Desktop.
Lynode dyndns
#!/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())
@espennilsen
Copy link
Author

[23:30] @irgeek Espen-_-: From http://www.linode.com/api/dns/domain%2Eresource%2Eupdate : "When Type=A or AAAA the token of '[remote_addr]' will be substituted with the IP address of the request."

This made my script alot easier.

@espennilsen
Copy link
Author

Fixed the filemode on the logger.

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