Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

EveryDNS update script (See related posts)

This script lets you update your IP address at everydns.net

#!/usr/bin/env ruby
require 'optparse' 
require 'net/http' 
require 'cgi' 

class EveryDnsUpdater
  attr_accessor :user, :pass
  attr_reader :ip, :code, :body

  def params
    @params ||= {"ver" => "0.1"}
  end
      
  def host
    "dyn.everydns.net"
  end
  
  def query
    "/index.php?" + params.collect{ |k,v| "#{k}=#{CGI.escape(v)}"}.join('&')
  end
  
  def parse(body)
    @body   = body
    @code   = @body.scan(/Exit Code: (\d+)/i).to_s.to_i
    @ip     = @body.scan(/IP now: ([\d\.]+)/).to_s
  end
      
  def run
    Net::HTTP.start(host) do |http| 

      req = Net::HTTP::Get.new(query) 
      req.basic_auth(user, pass)
      response = http.request(req)
      case response
      when Net::HTTPSuccess
        parse(response.body)
        return @code
      else
        return response.code
      end
    end
  end
end

opts = OptionParser.new 
updater = EveryDnsUpdater.new

opts.banner = "Usage: everydns [options]"
opts.separator ""
opts.on("-u", "--user=USER", "User name") { |str| updater.user = str } 
opts.on("-p", "--pass=PASS", "Password") { |str| updater.pass = str } 
opts.on("-d", "--domain=DOMAIN", "Domain name you wish to update")  { |str| updater.params["domain"] = str } 
opts.on("-a", "--address=[IP]", "IP address you want to supply") { |str| updater.params["ip"] = str } 
opts.on_tail("-h", "--help")   { puts opts; exit } 

opts.parse(ARGV)

if ARGV.empty? or updater.user.nil? or updater.pass.nil? or updater.params["domain"].nil?
  puts opts
  exit
end

if updater.run == 0
  puts "IP address set to #{updater.ip} successfully..."
else
  puts "Not successful..."
  puts updater.body
end

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts