Dynamic DNS with Route 53 II: For Fun and Profit
> Home

This is a follow up to the post Dynamic DNS with Route 53

The Setup

When working on web dev projects, it’s becoming increasingly common that the mobile layout of a website is well considered early on in a project, and I find that the easiest way to achieve this is to physically see the pages on a device.

The Issue

This can become problematic if dynamic addressing is involved if the development site is being served from a local client. To overcome this, you might start off the day by looking up the IP address of the development machine and then navigating the test device to it.

Should you have a short DHCP lease time, your address may change throughout the day, cause a repetition of this process.

Should you be using a framework such as Django, you might end up with something like ALLOWED_HOSTS += ['10.{}.{}.{}'.format(i,j,k) for i in range(256) for j in range(256) for k in range(256)] in your settings.py file to prevent a Host header mismatch error.

None of this is particularly glamorous.

But there is a better way!

The Solution

AWS IAM

My previous post details how to setup an IAM user with the required permissions: AWS IAM

Updating the Update Script

The Python script used in the previous post requests the current public IP address the request came from. However, for our local development environment we need the IP address of an interface on the machine the scrip is being run on.

All but two lines of the original script dealt with the uploading of the IP address to AWS, however determining the IP address of a local interface takes more than a web request.

It may be important to note that this solution currently will only use the first IPv6 address listed on an interface as determined by ifconfig. It is likely that there is a better solution than this if IPv6 is in use, however for the foreseeable future IPv4 is unlikely to be going anywhere at a LAN level, so this solution is likely to have use far into the future.

The changes to the script to determine a local IP address

#!/usr/bin/env python3

import subprocess
import re

# Configuration variables
INTERFACE_NAME = 'en0'
IPV = 4

# Get the ifconfig entry for the specified interface.
ifconfig = subprocess.run(["ifconfig", INTERFACE_NAME], stdout=subprocess.PIPE)

# Determine the IP address by regex.
if IPV == 4:
    re_pre = ".*inet"
    re_match = "(\d{1,3}\.){3}\d{1,3}"
    re_post = "netmask.*"

    regex = re.search("%s (%s) %s" % (re_pre, re_match, re_post), ifconfig.stdout.decode())

elif IPV == 6:
    re_pre = ".*inet6"
    re_match = "(\w{1,4}:{1,2}){1,7}\w{1,4}"

    regex = re.search("%s (%s) .*" % (re_pre, re_match), ifconfig.stdout.decode())

else:
    print("Invalid IP version")
    exit()

# Save the IP address.
ip_address = regex.groups()[0]

To configure supply the interface name, mine is en0 on my desktop machine, and the IP version.

Download the complete DynDNS_Local_R53.py script here.

As per the previous post, setup a job scheduler to run the script and you’re good to go!

Contents