Last active
July 21, 2022 13:13
-
-
Save imvenj/cc637f434508976f9ded06b49bd5eca1 to your computer and use it in GitHub Desktop.
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 | |
# Simple script that update namecheap ddns. | |
require 'open-uri' | |
require 'nokogiri' | |
require 'json' | |
HOST='' | |
DOMAIN='' | |
PASSWORD='' | |
IP_TMP='/tmp/.namecheap_ddns_ip' | |
class NamecheapDDNSUpdater | |
def initialize | |
json = open('http://myip.ipip.net/json').read | |
data = JSON.parse(json) | |
@ip = data['data']['ip'] | |
end | |
def run | |
if File.exists?(IP_TMP) | |
old_ip = open(IP_TMP).read | |
if old_ip == @ip | |
puts 'IP not changed, skip.' | |
return | |
end | |
end | |
save_ip | |
end | |
private | |
def update | |
url = "https://dynamicdns.park-your-domain.com/update?host=#{HOST}&domain=#{DOMAIN}&password=#{PASSWORD}&ip=#{@ip}" | |
doc = Nokogiri::XML(open(url)) | |
if doc.xpath('//ErrCount').text.to_i == 0 | |
puts 'DDNS updated.' | |
else | |
reason = doc.xpath('//errors/Err1').text | |
puts "DDNS update failed, reason: #{reason}." | |
end | |
end | |
def save_ip | |
update | |
open(IP_TMP, 'w') do |f| | |
f.write(@ip) | |
end | |
end | |
end | |
if __FILE__ == $0 | |
NamecheapDDNSUpdater.new.run | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment