Skip to content

Instantly share code, notes, and snippets.

@asolkar
Created March 22, 2013 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asolkar/5219693 to your computer and use it in GitHub Desktop.
Save asolkar/5219693 to your computer and use it in GitHub Desktop.
Dynamic DNS updater for Dreamhost
#!/usr/bin/env ruby
require 'net/http'
require 'securerandom'
require 'yaml'
class DreamhostIPUpdater
def initialize(apikey)
@api_url = 'https://api.dreamhost.com/';
@apikey = apikey
@ip = get_my_ip
@domains = get_my_domains
end
def get_my_ip
uri = URI.parse('http://checkip.dyndns.org/')
http = Net::HTTP.new(uri.host, uri.port)
response = http.get(uri.request_uri)
parts = response.body.match /(\d+\.\d+\.\d+\.\d+)/
return parts[1]
end
def https_request(url)
uuid = SecureRandom.uuid
uri = URI.parse(url + '&unique_id=' + uuid)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # read into this
response = http.get(uri.request_uri)
yaml_data = YAML::load_stream(response.body)
return yaml_data[0]['data']
end
def get_my_domains
response = https_request(
@api_url + '?key=' + @apikey +
'&format=yaml&cmd=dns-list_records&type=A&editable=1')
return response
end
def update_domains(domains)
@domains.each { |domain|
if domains.include?(domain['record'])
if domain['editable'] == 1
if domain['value'] == @ip
puts "#{domain['record']} : #{domain['value']} already is #{@ip}"
else
puts "Changing #{domain['record']} : #{domain['value']} -> #{@ip}"
update_domain(domain)
end
end
end
}
end
def remove_domain(domain)
puts " Removing #{domain['record']} : #{domain['value']}"
response = https_request(
@api_url + '?key=' + @apikey +
'&format=yaml&cmd=dns-remove_record&type=A&record=' +
domain['record'] + '&value=' + domain['value'])
if response == "record_removed"
puts " Info! #{domain['record']} : #{domain['value']} removed"
else
puts " ERROR! #{domain['record']} : #{domain['value']} not removed properly"
end
end
def add_domain(domain)
puts " Adding #{domain['record']} -> #{@ip}"
response = https_request(
@api_url + '?key=' + @apikey +
'&format=yaml&cmd=dns-add_record&type=A&record=' +
domain['record'] + '&value=' + domain['value'])
if response == "record_added"
puts " Info! #{domain['record']} : #{@ip} added"
else
puts " ERROR! #{domain['record']} : #{@ip} not added properly"
end
end
def update_domain(domain)
remove_domain(domain)
add_domain(domain)
end
end
updater = DreamhostIPUpdater.new('APIKEY')
updater.update_domains(['service1.yourhome.com','service2.yourhome.com'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment