namesilo dns update via python script and cron job on pfsense

I am using namesilo for my DNS, and they’ve got a solid little API system for stuff you can do to modify your DNS entries. So I use it as a poor mans dynamic DNS at home. For whatever reason though, their rrid changes every time you do an update, so you need to modify the URL to include the new rrid, which you pull from a dnsupdate api call. Thanks to a coworkers python skills, he wrote me this up, works great. Will only run the API call to update if the IP has changed. There are few things you must change for your own information though, and that is:

DOMAIN.TLDS (example: vivithemage.com) – there are 3 places to change this, line 7, 19, and line 39.
APIKEY (you get this from namesilo when you generate your API key) – there is one spot to change this, in line 39.
SUBDOMAIN (example: home) – there is one spot to change this, line 39.
SUB.DOMAIN.TLDS (example: home.vivithemage.com) – there are 3 spots to change this, line 26, 27 and 28.

Here’s the shell of the script:

#Must install requests package if you don't have items
# pip install requests

import requests
import xml.etree.ElementTree as ET

RECORD_IP_ADDRESS_URL = 'https://www.namesilo.com/api/dnsListRecords?version=1&type=xml&key=0c10aac5c78a9e2d1ad1e6&domain=DOMAIN.TLDS'
CURRENT_IP_ADDRESS_URL = 'http://whatismyip.akamai.com/'

#get current IP address from CURRENT_IP_ADDRESS_URL
current = requests.get(CURRENT_IP_ADDRESS_URL).content

print('Current IP address from akamai: %s' % current)
#read xml file
r = requests.get(RECORD_IP_ADDRESS_URL, allow_redirects=True)

xml = ET.fromstring(r.content)

#begin parsing xml for correct host (DOMAIN.TLDS)
for record in xml.iter('resource_record'):
#read host, value, and record_id from current record in xml
host = record.find('host').text
value = record.find('value').text
record_id = record.find('record_id').text

#if host is SUB.DOMAIN.TLDS, process further
if (host == 'SUB.DOMAIN.TLDS'):
print('SUB.DOMAIN.TLDS record IP address: %s' % value)

#if record IP address matches CURRENT_IP_ADDRESS_URL, do nothing
if (value == current):
print('Current IP address matches namesilo record')

#IP addresses don't match, let's update it
else:
print('IP addresses do not match, generating URL to update')

#place the record_id in the url
new_URL = 'https://www.namesilo.com/api/dnsUpdateRecord?version=1&type=xml&key=APIKEY&domain=DOMAIN.TLDS&rrid='+record_id+'&rrhost=SUBDOMAIN&rrvalue='+current+'&rrttl=3600'
print(new_URL)

#send request to URL
new = requests.get(new_URL)

#print the xml reply, this doesn't need to be pretty
print(new.content)

Now if you are like me, and use pfsense, you have to install a module, which you can do by running these commands in shell:
python2.7 -m ensurepip
python2.7 -m pip install requests
python2.7 -m pip install –upgrade pip

Once you run that, chmod +x your .py script and you are good to go to add the script to a cronjob. DO make sure it’s in a directory you can run as the user, and modify permissiosn to make sure.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

  

  

  

This site uses Akismet to reduce spam. Learn how your comment data is processed.