Skip to content

Instantly share code, notes, and snippets.

@stef-levesque
Last active August 29, 2015 14:00
Show Gist options
  • Save stef-levesque/11229362 to your computer and use it in GitHub Desktop.
Save stef-levesque/11229362 to your computer and use it in GitHub Desktop.
Small python script to update Enom Dynamic DNS
import requests
from xml.etree.ElementTree import XML
#-------------------------------------------------------------------------------
IP_CHECK_URL = 'http://ip-api.com/json'
IP_TEXT_FILE = '/home/hannibal/updateip.txt'
ENOM_INTERFACE_URL = 'https://reseller.enom.com/interface.asp'
DOMAIN = 'yourdomain.net'
PASSWD = 'xxxxxxxx'
ENOM_PARAMS = { 'command': 'setdnshost', 'responsetype': 'xml' };
PUSH_TOKEN = 'xxxxxxxx'
PUSH_USERID = 'xxxxxxxx'
#-------------------------------------------------------------------------------
def get_remote_ip():
r = requests.get(IP_CHECK_URL)
j = r.json()
ip = j['query'].encode('utf8')
return ip
#-------------------------------------------------------------------------------
def get_local_ip():
return open(IP_TEXT_FILE, 'r').read()
#-------------------------------------------------------------------------------
def update_host_ip(new_ip):
ENOM_PARAMS['zone'] = DOMAIN
ENOM_PARAMS['domainpassword'] = PASSWD
ENOM_PARAMS['address'] = new_ip
r = requests.get(ENOM_INTERFACE_URL, ENOM_PARAMS)
return XML(r.text).find('Done').text == 'true'
#-------------------------------------------------------------------------------
def send_pushover_message(message):
pushover_url = 'https://api.pushover.net/1/messages.json'
payload = {'token': PUSH_TOKEN, 'user': PUSH_USERID, 'message' : message }
return requests.post(pushover_url, data=payload).json()['status'] == 1
#-------------------------------------------------------------------------------
def alert_new_ip(new_ip, succeeded):
text = 'UpdateIP.py - '
if succeded:
text += 'Success - '
else:
text += 'Failed - '
text += 'IP: ' + new_ip
# use Pushover or email to alert the new ip
send_pushover_message(text)
#-------------------------------------------------------------------------------
def main():
# get ip addresses
remote_ip = get_remote_ip()
local_ip = get_local_ip()
# compare remote with local
if remote_ip == local_ip:
return
succeeded = update_host_ip(remote_ip)
alert_new_ip(remote_ip, succeeded)
#-------------------------------------------------------------------------------
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment