Skip to content

Instantly share code, notes, and snippets.

@jaderobbins
Created September 4, 2009 16:50
Show Gist options
  • Save jaderobbins/180988 to your computer and use it in GitHub Desktop.
Save jaderobbins/180988 to your computer and use it in GitHub Desktop.
A ruby script that acts like DynDns for your own domain (via DreamHost)
require 'open-uri'
require 'rubygems'
require 'json'
# Change these settings
hostname = 'home.hostname.com' # the hostname you use to connect to home ip
apikey = 'your-dreamhost-api-key' # a dreamhost api key that has access to all dns functions
storedIP = ''
print 'Getting current external ip: '
ip = open('http://checkip.dyndns.com'){ |f| /([1-9]{1,3}\.){3}[0-9]{1,3}/.match(f.read)[0].to_a[0] }
puts ip
print 'Getting stored ip from dreamhost for ' + hostname + ': '
dns = open( 'https://api.dreamhost.com/?key=' + apikey +
'&cmd=dns-list_records&format=json&unique_id=' + Time.now.to_f.to_s,
'UserAgent' => 'RubyWget').read
result = JSON.parse(dns)
result['data'].each do |d|
if d['record'] == hostname
storedIP = d['value']
end
end
puts storedIP
if ip != storedIP
puts 'IP addresses do not match, changing Dreamhost ip for ' + hostname + ' from ' + storedIP +
' to ' + ip + '.'
dns = open( 'https://api.dreamhost.com/?key=' + apikey +
'&cmd=dns-remove_record&record=' + hostname + '&type=a&value=' + storedIP +
'&unique_id=' + Time.now.to_f.to_s,'UserAgent' => 'RubyWget').read
print 'Removing current DNS entry: '
puts dns
dns = open( 'https://api.dreamhost.com/?key=' + apikey +
'&cmd=dns-add_record&record=' + hostname + '&type=a&value=' + ip +
'&unique_id=' + Time.now.to_f.to_s,'UserAgent' => 'RubyWget').read
print 'Adding new DNS entry: '
puts dns
else
puts 'IPs addresses match. No need to DNS'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment