Cory O'Daniel – These are just words Software development, thoughts, and randomness

6Jul/090

Ruby Dynamic DNS Script for Slicehost

I 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#! /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

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.