February 7, 2022

Creating My Own Dynamic DNS Using Porkbun API

Most people don’t have a static IP with their home internet service. Some ISPs do offer you one for an added fee each month, but for some reason my ISP thinks that is somehow worth a $75 setup fee and then an addition $15 a month. Considering the monthly bill is $65, that seems a like a bit much. I don’t strictly need a static IP, but it would be nice to have a memorable address to remotely access my home network. Think something like remote.mydomain.com rather than 97.234.76.276. I started looking at the various services out there that provide Dynamic DNS services with small apps that will automatically update upon your IP changing. These services are pretty nice, but are also a monthly fee, so at the end of the day why not just go the static IP route? They also generally have weird urls that I am not really a fan of, something like yourname.ninjadnscoffeeaddict.net. I would prefer to just use my own domain.

I never really thought about doing this myself until I realized that my awesome domain provider, Porkbun, has an API that allows you to programmatically update aspects of DNS records! That’s pretty sweet, huh? I got to work with some web searches and used Paw, which is part of Setapp to start messing with some calls to see if I could get something figured out. After a bit of fiddling, trial and error, and some more searching, I ended up getting a script working that would do the following:

  1. Check to see what my current WAN IP is
  2. Make a call out to Porkbun with my credentials and update sub.domain.com with the WAN IP from step one above.

This is what that script looks like. Bare with me, I know it’s probably not the best scripting out there.

#!/bin/bash

#grab wan ip from canhazip and set variable to output
output=$(curl http://canhazip.com)

#display wan ip for troubleshooting
echo "$output";

#wait 2 seconds
sleep 2s

#update dns ip of sub.domain.com via porkbun api
curl \
--header "Content-type: application/json" \
--request POST \
--data '{"secretapikey":"sectret_key","apikey":"my_key","content":"'$output'"}' \
https://porkbun.com/api/json/v3/dns/editByNameType/domain.com/A/sub

I then made this script run on a semi-frequent basis via a launch daemon. Since the IP from my ISP only changes every few weeks (or after a reboot of my gateway), I have it set to update once every 12 hours. This is not a mission critical feature so If I have to wait a bit for it to get sorted, it’s fine. I assume it’s obvious this is not mission critical given the way I am going about this. 😬

What would make this better? Well, a couple things.

  1. I really should be checking to see if the IP has even changed, and if it hasn’t, don’t push an update.
  2. The call I am using to get the WAN IP is hacky and should probably be done by pulling from inside vs outside.

Not usre if this is actually useful for anyone, but I wanted to make it public in case it triggers some other ideas for people. 😀👍


Porkbun API DNS IP WAN


Previous post
Stop Driving Wedges. This was originally written in December 2021 for the Hemispheric Newsletter as part of One Prime Plus for the Hemispheric Views podcast. Not
Next post
Read Like No One Is Watching This was originally written in February 2022 for the Hemispheric Newsletter as part of One Prime Plus for the Hemispheric Views podcast. With the
Jason Burk | 1999-2022
blot.im! | 🧛‍♂️
📦 Blog Archives 📦

#FreeAds
top