I bought a new (arguably better) domain for my blog now! If you’re reading this, you’ve probably noticed, but it is therubyist.org, because I’m a fan of Ruby. The old name won’t be going anywhere, at least for the time being. Given the purchase of this new domain, I have several domains I need to maintain. Since I run this blog (and a few other services) from my home server which has a dynamic IP, setting up the domain apex (i.e., “naked domain”) is tricky. I’ve been using a dynamic DNS service called duckdns.org which gets the job done for subdomains since I can just CNAME to my personal DuckDNS subdomain, but it doesn’t solve this apex problem.
I decided that I was tired of doing this manually and that I would try to write a script. It would be appropriate, given my blog’s new domain name, to write it in Ruby. I did a quick DuckDuckGo search and noticed that someone wrote a GoDaddy SDK for Ruby. Yes, I use GoDaddy for my DNS… not necessarily endorsing them, but they seem to work well for my needs, especially now that I’ve discovered that they offer RESTful APIs.
First, I needed to set up some API keys. That’s done here. Create a key and secret and keep them handy. Create a script (I called mine set_domain_apex.rb
) with something like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#!/usr/bin/env ruby require 'godaddy/api' # An array of domains that need to be managed @domains = [ 'therubyist.org', 'gnagy.info' ] # Feel free to put these somewhere else... @apikey = 'IEOIuw89wiuwoiuIPW_798738' @apisecret = 'yUIKhJWHEjkwiulmwq989' # This is the network interface on this machine with a public IP @public_int = 'enp5s0' # A regular expression for determining an IPv4 address from `ip` output @ip_regex = 'inet\s[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' # Determine the server's public IP @public_ip = `ip address show dev #{@public_int} | grep -oE '#{@ip_regex}' | awk '{ print $2 }'`.chomp # Connect to the GoDaddy API @api = Godaddy::Api.new @apikey, @apisecret # This is how we'll submit DNS record updates def configure_apex(domain, ip) uri = "/v1/domains/#{domain}/records/A/@" @api.put( uri, [{ type: 'A', name: '@', data: ip, ttl: 600 }] ) end # Actually do the work of updating DNS entries @domains.each do |dom| puts "Setting #{dom} apex to #{@public_ip}" configure_apex(dom, @public_ip) end |
Running this should output something like…
1 2 3 4 |
Setting therubyist.org apex to 1.2.3.4 Setting gnagy.info apex to 1.2.3.4 |
That’s it! Now, just set up cron to run this script every hour or so (or get really fancy and making it run whenever the DHCP client updates your IP). Enjoy.
One thought on “Updating GoDaddy DNS Entries with Ruby”
Comments are closed.