Skip to content

Instantly share code, notes, and snippets.

@krevels
Created January 21, 2014 19:52
Show Gist options
  • Save krevels/8547104 to your computer and use it in GitHub Desktop.
Save krevels/8547104 to your computer and use it in GitHub Desktop.
Refresh dreamhost dns entry with public ip
require 'uri'
require 'net/http'
DREAMHOST_API_KEY = ENV['DREAMHOST_API_KEY']
DNS_RECORD = "host.example.com"
TIMER = 600
abort "missing api key" if DREAMHOST_API_KEY.nil?
Process.daemon
loop do
pid = Process.fork do
begin
# find current public ip
ip = Net::HTTP.get(URI('http://bot.whatismyipaddress.com'))
raise "can't find ip" if ip.nil?
uri = URI("https://api.dreamhost.com")
# remove existing dns entry
uri.query = URI.encode_www_form :key => DREAMHOST_API_KEY, :cmd => "dns-remove_record", :record => DNS_RECORD, :type => "A", :value => ip
Net::HTTP.get uri
# add dns entry
uri.query = URI.encode_www_form :key => DREAMHOST_API_KEY, :cmd => "dns-add_record", :record => DNS_RECORD, :type => "A", :value => ip
Net::HTTP.get uri
rescue
puts "ruh-roh"
end
end
Process.waitpid(pid)
sleep TIMER
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment