Created
May 26, 2015 06:49
-
-
Save qhwa/4a54bf5990c3c9c6831b to your computer and use it in GitHub Desktop.
automatically update DNSPod record with internal IP address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'socket' | |
| require 'net/http' | |
| require 'net/https' | |
| require 'open-uri' | |
| require 'logger' | |
| class AutoDNS | |
| PARAMS = { | |
| "login_email" => "YOUR_LOGIN_EMAIL", | |
| "login_password" => "YOUR_LOGIN_PASSWORD", | |
| "domain_id" => YOUR_DOMAIN_ID, | |
| "record_id" => YOUR_RECORD_ID, | |
| "sub_domain" => "pcduino", | |
| "record_line" => "默认", | |
| "format" => "json", | |
| "value" => "" | |
| } | |
| def update_ip | |
| ip = internal_ip | |
| logger.info { "set record IP: #{ip}" } | |
| url = URI.parse('https://dnsapi.cn/Record.Ddns') | |
| http = Net::HTTP.new(url.host, url.port) | |
| http.use_ssl = true | |
| request = Net::HTTP::Post.new(url.path) | |
| request.set_form_data(PARAMS.merge value: ip) | |
| logger.debug { http.request(request).body } | |
| return true | |
| end | |
| def internal_ip | |
| Socket.ip_address_list.find { |intf| intf.ipv4_private? }.ip_address | |
| end | |
| private | |
| def logger | |
| @logger ||= ::Logger.new(STDOUT) | |
| end | |
| end | |
| if __FILE__ == $0 | |
| AutoDNS.new.update_ip | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment