Ruby Dynamic DNS Script for Slicehost
Monday, July 6th, 2009I know everyone and there mother has written one of these, so I guess I’m now my mother. Run this as a cron to enable a pretty cheapo dynamic DNS via Slicehost. You’ll need the ActiveResource gem to use it.
#! /usr/bin/env ruby
# */5 * * * * /usr/bin/dynamicdns.rb
require 'rubygems'
require 'activeresource'
require 'open-uri'
# Configure your stuff
@api_key = 'YOUR_API_KEY'
@record_id = 1337 #the record ID you are updating
@ttl = 10 * 60 #ten minutes, yeah sure.
@record_type = 'A'
@record_name = 'vpn'
@ip_site = 'http://checkip.dyndns.org/'
@ip_path = '/tmp/lastip'
@log = '/tmp/lastip.log'
API_SITE = "https://#{@api_key}@api.slicehost.com/"
class Record < ActiveResource::Base;self.site = API_SITE;end;
# Check the last ip address
@last_ip = File.exist?(@ip_path) ? File.read(@ip_path).strip : ''
begin
@ip = open(@ip_site){|f| f.read}.match(/[0-9.]+/)[0]
rescue Exception
File.open(@log,'a+'){|f| f.puts "#{Time.now} [ERROR] - Failed to open #{@ip_site}"}
exit(1)
end
# Update the email address
if @ip != @last_ip
File.open(@ip_path,'w+'){|f| f.puts @ip}
record = Record.find(@record_id)
record.name = @record_name
record.record_type = @record_type
record.data = @ip
record.ttl = @ttl
record.save
File.open(@log,'a+'){|f| f.puts "#{Time.now} [INFO] - IP updated: #{@ip}"}
else
File.open(@log,'a+'){|f| f.puts "#{Time.now} [INFO] - IP did not change"}
end




