Skip to content

Instantly share code, notes, and snippets.

@r4um
Created October 28, 2012 12:49
Show Gist options
  • Save r4um/3968517 to your computer and use it in GitHub Desktop.
Save r4um/3968517 to your computer and use it in GitHub Desktop.
Dynamic DNS client updater for no-ip.com service
#!/usr/bin/env ruby
#
# Dynamic DNS client updater for no-ip.com service
#
require 'rubygems'
require 'logger'
require 'net/http'
require 'optparse'
require 'uri'
class NOIP
VERSION = '0.1'
UPDATE_HOST = "dynupdate.no-ip.com"
MYIP_HOST = "ip1.dynupdate.no-ip.com"
def self.MyIP
Net::HTTP.get(URI.parse("http://#{MYIP_HOST}"))
end
def self.Update(host, user, pass, myip=nil)
uri = URI.parse("https://#{UPDATE_HOST}/nic/update")
uri.query = "hostname=#{host}"
uri.query += "&myip=#{myip}" if myip
ua = "noip.rb/#{VERSION} pranay.kanwar@gmail.com"
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == "https"
http.ssl_version = :TLSv1
req = Net::HTTP::Get.new(uri.request_uri, {"UserAgent" => ua})
req.basic_auth user, pass
res = http.start {|h| h.request(req) }
res.body
end
def self.run(args)
options = {}
options[:myip] = false
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{__FILE__} [options]"
opts.separator ""
opts.on("-h", "--host HOSTNAME", "Update HOSTNAME") do |host|
options[:host] = host
end
opts.on("-u", "--username USERNAME", "Access USERNAME") do |user|
options[:user] = user
end
opts.on("-p", "--password PASSWORD", "Access PASSWORD") do |pass|
options[:pass] = pass
end
opts.on("-m", "--myip", "Use #{MYIP_HOST} to get IP address") do
options[:myip] = true
end
opts.on_tail("-H", "--help", "Show this message") do
puts opts
exit
end
end
log = Logger.new(STDOUT)
begin
opts.parse!(args)
myip = nil
raise OptionParser::MissingArgument, "-h, no host given."\
if not options[:host]
raise OptionParser::MissingArgument, "-u, no username given."\
if not options[:user]
raise OptionParser::MissingArgument, "-p, no password given."\
if not options[:pass]
if options[:myip]
log.info "Requesting IP Address from #{MYIP_HOST}"
myip = MyIP()
log.info "IP Address #{myip}"
end
resp = Update(options[:host], options[:user], options[:pass], myip)
log.info "Resposnse #{resp}"
rescue Errno::ECONNRESET
# ignore
rescue SystemExit
exit
rescue Exception => e
log.fatal "#{e.class} #{e.message}"
exit
end
end
end
NOIP.run(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment