DNSPod 动态解析(DDNS) 的 Ruby 版本

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

root@ubuntu:~# cat syncip.rb 
#!/usr/bin/env ruby
#encoding:utf-8
#code by johnlen shaw
require 'socket' 
require 'net/http' 
require 'net/https' 
require 'time' 
require 'open-uri' 
require 'net/telnet'

$parms = {
	"login_email" => "xxx@live.com",
	"login_password" => "xxxxxx",
	"format" => "json",
	"domain_id" => 1012096,
	"record_id" => 15271821,
	"sub_domain" => "d610",
	"record_line" => "默认",
	"value" => ""
}

current_ip = nil

def getip
	begin
		server = Net::Telnet::new('Host' => 'ns1.dnspod.net',
        		                  'Port' => 6666,
        	        	          'Telnetmode' => false)
		server.cmd("\n") do |response|
  			return response
		end
	rescue Exception => ex
		sleep(30)
		getip
        end
end

def ddns(ip)
	$parms["value"] = ip.chomp.to_s
	url = URI.parse('https://dnsapi.cn:443/Record.Ddns')
	http = Net::HTTP.new(url.host, url.port)
	http.use_ssl = true if url.scheme == 'https'
	request = Net::HTTP::Post.new(url.path)
	request.set_form_data($parms)
	puts http.request(request).body
	return true
end

if $0 == __FILE__
#	begin
		loop do
			puts ip = getip
			if current_ip != ip then
				if ddns(ip) then
					current_ip = ip
				end
			end
			sleep(60)
		end
#	rescue	Exception => ex
#		p ex
#		return "Debug and fix me please."	
#	end
end