#!/usr/bin/ruby require 'net/http' Net::HTTP.version_1_2 # BEGIN ===== User setup section ===== # Account information ID = 'your_id' PASSWD = 'your_passwd' HOST = 'your.host.com' # How to get my current global IP ? # If you can't understand what you should do, # please comment out following paragraph, # and simply write down, IP_CURRENT = nil. IP_CURRENT = Proc.new{ /(?:\d{1,3}\.){3}\d{1,3}/ =~ `/sbin/ifconfig ppp0` $& } # Program type setting # auto background update(IS_DAEMON = true) / user manual update(IS_DAEMON = false) ? IS_DAEMON = false UPDATE_PEROID = 60 * 60 #[sec] LOG_FILE = 'ip-updater.log' # END ===== User setup section ===== # Target Server URL SRV_UPDATE_HOST = 'www.mydns.jp' SRV_UPDATE_PATH = '/login.html' # About this program AGENT_NAME = 'Ruby-Updater' AGENT_VERSION = '0.2' AGENT_MODE = 'MD5' # main program begins from the next line. def update(ip_previous = nil) print("#{Time.now.to_s}, Trying...\n") ip_current = nil if IP_CURRENT ip_current = IP_CURRENT.call end # check IP whether the DDNS registered IP equals my current global IP. if ip_previous and ip_previous == ip_current then print "MESSAGE: No need to update, now DDNS.IP == IP_CURRENT\n" return ip_current end req = Net::HTTP::Get::new(SRV_UPDATE_PATH) req.basic_auth ID, PASSWD Net::HTTP.start(SRV_UPDATE_HOST) {|http| response = http.request(req) response.body.each{|line| if line =~ /(\d{1,3}\.){3}\d{1,3}/ then ip_current = $& break end } } print "MESSAGE: registered DDNS.IP as #{ip_current}\n" return ip_current end if IS_DAEMON then exit! if fork # fork # log file setting log_file = File.open(LOG_FILE, 'w+') STDOUT.reopen(log_file) STDOUT.sync = true STDERR.reopen(log_file) STDERR.sync = true Signal.trap('HUP'){exit} Signal.trap('TERM'){exit} END{log_file.close} print("PID: #{Process.pid}\n") print("Start #{AGENT_NAME} at #{Time.now.to_s}\n") ObjectSpace.each_object(IO){|io| io.close unless io.closed? || io == log_file || io == STDOUT || io == STDERR } Process.setsid ip = nil loop{ ip = update(ip) sleep(UPDATE_PEROID) } else update end