Categories
Programming python python

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: swamp.xyz) – there are 4 places to change this, line 11, 31, 32, and 43.
APIKEY (you get this from namesilo when you generate your API key) – there are two spots to change this, in line 11 and 43.
SUBDOMAIN (example: va) – there are two spots to change this: line 11 and 43.
SUB.DOMAIN.TLDS (example: va.swamp.xyz) – there are 3 spots to change this, line 30,31, and 32.

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

#print the xml reply, this doesn't need to be pretty
print(new.content)#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=xxxxxxxxxxxxxxxxxxx&domain=swamp.xyz'
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 (swamp.xyz)
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 va.swamp.xyz, process further
if (host == 'va.swamp.xyz'):
print('va.swamp.xyz 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=xxxxxxxxxxxxxxxxxxxxx&domain=swamp.xyz&rrid='+record_id+'&rrhost=va&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. I added it via the pfsense cron gui:

*/5 * * * * root /usr/local/bin/python2.7 /usr/local/namesilo_update.py

Do make sure it’s in a directory you can run as the user, and modify permissions to make sure.

4 replies on “namesilo dns update via python script and cron job on pfsense”

Some better formatting for the code would be quite nice. It looks like an indent after the for until the end. Then every if adds an indent

Also add and type == ‘A’ to the line
“if (host == ‘SUB.DOMAIN.TLDS’):”
so it is
“if (host == ‘SUB.DOMAIN.TLDS’ and type == ‘A’ ):”
That way it doesn’t overwrite txt records and such.

Leave a Reply

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