Last active
September 26, 2015 02:18
-
-
Save alvin2ye/1022961 to your computer and use it in GitHub Desktop.
事先检查可以防止dyndns 抛 abuse
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/ruby | |
| # 自动同步 dyndns | |
| # 事先检查可以防止dyndns 抛 abuse | |
| DOMAIN = "xxx.dyndns-ip.com" | |
| USER_PWD = "xxx:yyy" | |
| IPREGX = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ | |
| def remote_ip | |
| @remote_ip ||= begin | |
| `curl -qs --connect-timeout 5 http://checkip.dyndns.org` =~ IPREGX | |
| $~ | |
| end | |
| end | |
| def local_ip | |
| @local_ip ||= begin | |
| `ping -c 1 #{DOMAIN}` =~ IPREGX | |
| $~ | |
| end | |
| end | |
| def sync_ip | |
| cmd = <<-EOF | |
| curl -sq -u "#{USER_PWD}" --connect-timeout 5 \ | |
| "https://members.dyndns.org/nic/update?hostname=#{DOMAIN}&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG" | |
| EOF | |
| puts cmd | |
| `#{cmd}` | |
| end | |
| puts " remote_ip #{remote_ip} " | |
| puts " local_ip #{local_ip} " | |
| unless remote_ip && local_ip | |
| puts "get ip faile" | |
| exit 0 | |
| end | |
| if remote_ip.to_s == local_ip.to_s | |
| puts "same" | |
| else | |
| puts "not same, do sync" | |
| puts sync_ip | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment