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: – 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: – 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


#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 (
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, process further
if (host == ''):
print(' 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
print('IP addresses do not match, generating URL to update')

#place the record_id in the url
new_URL = ''+record_id+'&rrhost=va&rrvalue='+current+'&rrttl=3600'

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

#print the xml reply, this doesn't need to be pretty

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/

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.