Skip to content

Instantly share code, notes, and snippets.

@alvin2ye
Last active September 26, 2015 02:18
Show Gist options
  • Select an option

  • Save alvin2ye/1022961 to your computer and use it in GitHub Desktop.

Select an option

Save alvin2ye/1022961 to your computer and use it in GitHub Desktop.
事先检查可以防止dyndns 抛 abuse
#!/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