Created
July 30, 2009 16:30
-
-
Save caius/158767 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# update_slicehost_dns.rb | |
# Updates the A record of a domain to your | |
# current IP address | |
# | |
# Created by Caius Durling on 2009-07-30. | |
# Copyright 2009 Caius Durling. | |
# | |
# MIT Licence | |
# Change these to your details | |
my_api_key = "blahblahblah" | |
domain = "example.com." | |
# Ignore all this unless you're interested | |
require "rubygems" | |
require "activeresource" | |
require "open-uri" | |
SITE = "https://#{my_api_key}@api.slicehost.com" | |
class Zone < ActiveResource::Base | |
self.site = SITE | |
def records | |
Record.find(:all, :params => {:zone_id => self.id}) | |
end | |
end | |
class Record < ActiveResource::Base | |
self.site = SITE | |
end | |
# Find the domain, then the A record for said domain | |
zone = Zone.find(:first, :params => {:origin => domain}) | |
exit(1) if zone.nil? | |
a_record = zone.records.select {|r| r.record_type = "A" }.first | |
exit(2) if a_record.nil? | |
# Then the current IP address | |
dns_ip = a_record.data | |
# Grab the current IP from akamai | |
live_ip = open("http://whatismyip.akamai.com/").read | |
# Check if the IP has changed | |
if live_ip != dns_ip | |
# We need to update it! | |
a_record.data = live_ip | |
a_record.save | |
puts "Updated A record from '#{dns_ip}' to '#{live_ip}'" | |
else | |
puts "No update needed" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment