Skip to content

Instantly share code, notes, and snippets.

Created May 13, 2013 20:25
Show Gist options
  • Save anonymous/5571211 to your computer and use it in GitHub Desktop.
Save anonymous/5571211 to your computer and use it in GitHub Desktop.
Dynamic DNS script to update DNS record whenever IP address changes.
#!/usr/bin/env ruby
require 'fog'
require 'open-uri'
class BrandsmithDNS
def initialize
@dns = Fog::DNS.new(:provider => 'rackspace', :rackspace_api_key => 'xxxxxxxxxxxxxxxxxx', :rackspace_username => 'yyyyyyyyyyyyyyyyyy')
@record = @dns.zones.get('11111111').records.get('A-222222222')
end
def dns_record_ip
@dns_record_ip ||= @record.value
end
def actual_ip
@actual_ip ||= open('http://whatismyip.akamai.com').read
end
def needs_update
dns_record_ip != actual_ip
end
def update_record
@record.value = actual_ip
@record.save
end
end
dns = BrandsmithDNS.new
puts "Current IP address: #{dns.actual_ip}"
puts " DNS Record: #{dns.dns_record_ip}"
if dns.needs_update
print "Updating..."
dns.update_record
puts "done."
else
puts "Nothing to update."
end
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>updatedns</string>
<key>Disabled</key>
<false/>
<key>UserName</key>
<string>nobody</string>
<key>StartInterval</key>
<integer>1800</integer>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/updatedns</string>
</array>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment