almost human fully human

SOLUSIPSE

/var/log

Own DDNS service powered by Digital Ocean

Although Digital Ocean provide its own DNS servers, it’s possible to set a simple DDNS, since they provide a handy api for domain settings manipulation. I’ve written a small script that has to be installed on every machine, which IP we want to track. Remember to modify token, domain_name and record_name variables.

'''
doddns
This script was made for using a Digital Ocean's droplet as a ddns service.
[!] Remember to put your credentials below.
[!] Script needs requests library. To install it use pip: pip install requests.
Before you'll start, check if it works properly with your config. Simply run it:
python doddns.py
Use cron to execute it every x minutes. There's an example of 5 minutes interval:
*/5 * * * * /usr/bin/python /home/user/dodns.py
This script was released into the public domain.
Contact me: solusipse.net
'''

#------------------------------------------------------------------------------
# EDIT HERE

token = "insertyourtokenhere"
domain_name = "exampledomain.com"
record_name = "examplerecord"

# EDIT HERE
#------------------------------------------------------------------------------

import requests, re
from requests.auth import HTTPBasicAuth

headers = { "Authorization": "Bearer " + token }

def get_ip():
    return re.findall( r'[0-9]+(?:\.[0-9]+){3}', requests.get("http://ipinfo.io/ip").text )[0]

def get_record_id(domain):
    records_url = "https://api.digitalocean.com/v2/domains/%s/records/?per_page=200" % domain

    r = requests.get(records_url, headers=headers).json()

    for a in r['domain_records']:
        if a['name'] == record_name:
            return (domain, a['id'])

def set_record_ip(recdata):
    record_url = "https://api.digitalocean.com/v2/domains/%s/records/%s" % recdata
    r = requests.put(record_url, headers=headers, data={"data": str(get_ip())})
    return r.status_code

if set_record_ip(get_record_id(domain_name)) == 200:
    print("Done. Address for record %s of domain %s set to %s." % (record_name, domain_name, get_ip()))