Created
October 12, 2018 21:40
-
-
Save owenjones/4331ac1f7d72a7eccb9e04065ef2ad6b to your computer and use it in GitHub Desktop.
Digital Ocean API - Dynamic IP Record Updater
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/env python3 | |
domain = "" # Root domain | |
record = "" # Record to update | |
token = "" # API token (needs write access) | |
try : | |
import os.path | |
from urllib.request import urlopen | |
from urllib.error import URLError | |
from base64 import b64decode as decode | |
import digitalocean # pip install python-digitalocean | |
except ImportError as e : | |
raise Exception("Missing package: " + str(e)) | |
try : | |
ip = urlopen("http://myip.dnsdynamic.org/").read().decode("UTF-8") | |
except URLError : | |
exit("Could not obtain current IP address (timeout)") | |
if os.path.isfile("lastaddress") : | |
with open("lastaddress", "r") as ipcheck : | |
lastaddress = ipcheck.read() | |
if(ip == lastaddress) : | |
exit(0) | |
do = digitalocean.Domain(token=token, name=domain) | |
rec = do.get_records() | |
for r in rec : | |
if r.name == record : | |
r.data = ip | |
r.save() | |
with open("lastaddress", "w") as ipcheck : | |
ipcheck.write(ip) | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment