Created
June 5, 2019 08:19
-
-
Save digitronik/2e4f4e5a3fa3d9009f7750b4e63e1e08 to your computer and use it in GitHub Desktop.
Manage GoDaddy DNS with simple python script. This script help you to point godaddy to your dynamic public IP.
This file contains 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
from datetime import datetime | |
from urllib import request, error | |
from godaddypy import Client, Account | |
# Manage GoDaddy DNS with simple python script. | |
# This script help you to point godaddy to your dynamic public IP. | |
# Get the Production API key/secret from https://developer.godaddy.com/keys/. | |
# Ensure it's for "Production". | |
KEY = "your key" | |
SECRET = "your secret" | |
# Domain to update. | |
DOMAIN = "your domain like example.com" | |
# Advanced settings - change only if you know what you're doing :-) | |
# Record type, as seen in the DNS setup page, default A. | |
TYPE = "A" | |
# Writable path to last known Public IP record cached. Best to place in tmpfs. | |
CachedIP = "/tmp/current_ip" | |
# External URL to check for current Public IP, must contain only a single plain text IP. | |
# examples http://api.ipify.org, http://ip.42.pl/raw etc... | |
CheckURL = "http://ip.42.pl/raw" | |
class GoDaddy(object): | |
def __init__(self, key=KEY, secret=SECRET): | |
self.key = key | |
self.secret = secret | |
self.acct = Account(api_key=key, api_secret=secret) | |
self.client = Client(self.acct) | |
@property | |
def current_gd_ip(self): | |
out = self.client.get_records(DOMAIN, record_type=TYPE) | |
return str(out[0].get("data")) | |
@property | |
def current_my_ip(self): | |
try: | |
return request.urlopen(CheckURL).read().decode() | |
except error.URLError: | |
print("Error: Fail to get Public IP... check connection") | |
return None | |
def update_ip(self): | |
if self.current_my_ip != self.current_gd_ip: | |
self.client.update_ip(self.current_my_ip, domains=[DOMAIN]) | |
class IpLog(object): | |
def __init__(self, path=CachedIP): | |
self.path = path | |
def write(self, ip): | |
with open(self.path, "a+") as f: | |
f.write("{}:{}\n".format(datetime.now(), ip)) | |
def read(self): | |
try: | |
with open(self.path, "r") as f: | |
last_line = f.readlines()[-1] | |
ip = last_line.split(":")[-1] | |
return ip.strip() | |
except FileNotFoundError: | |
return None | |
if __name__ == "__main__": | |
gd = GoDaddy() | |
log = IpLog() | |
while True: | |
if gd.current_my_ip != log.read(): | |
gd.update_ip() | |
log.write(gd.current_my_ip) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need to install the third party package as a requirement