Created
June 22, 2015 11:05
-
-
Save solusipse/1e73912235f735254519 to your computer and use it in GitHub Desktop.
Own DDNS service on Digital Ocean
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
''' | |
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())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment