Created
January 16, 2009 10:17
-
-
Save henrik/47890 to your computer and use it in GitHub Desktop.
Ruby script to update dynamic DNS on Swedish LoopiaDNS.
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 | |
# Updates dynamic DNS on Swedish LoopiaDNS. | |
# Run in crontab, e.g.: | |
# */15 * * * * ruby /usr/local/bin/update_loopia_dyndns.rb | |
USERNAME = "foo.com" | |
PASSWORD = "bar" | |
DOMAINS = %w[ | |
foo.com | |
bar.com | |
baz.com | |
] | |
IP_URL = "http://whatismyip.org/" | |
#IP_URL = "http://ip.fruktsallad.net/" | |
USER_AGENT = "UpdateLoopiaDynDNS" | |
STORAGE = "/tmp/external_ip" | |
class IPQuery | |
require "open-uri" | |
IP_RE = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ | |
def self.external_ip | |
external_ip = open(IP_URL, "User-Agent" => USER_AGENT).read | |
raise "Did not get an IP! Got: #{external_ip.inspect}" unless external_ip =~ IP_RE | |
external_ip | |
end | |
end | |
class LoopiaDNS | |
require "net/https" | |
# See http://support.loopia.co.yu/fajlovi/dynsite/loopiadns.dns | |
ACCEPTABLE_RESPONSES = %w[good nochg] | |
def initialize(username, password, domain) | |
@username = username | |
@password = password | |
@domain = domain | |
end | |
def dyndns_ip=(ip) | |
http = Net::HTTP.new('loopiadns.loopia.se', 443) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
http.start do |http| | |
request = Net::HTTP::Get.new("/XDynDNSServer/XDynDNS.php?hostname=#{@domain}&myip=#{ip}") | |
request.basic_auth @username, @password | |
response = http.request(request).body | |
raise "Bad reply from Loopia. Reply: #{response.inspect}" unless ACCEPTABLE_RESPONSES.include?(response) | |
end | |
end | |
end | |
# Update IP if it has changed | |
external_ip = IPQuery.external_ip | |
unless File.exists?(STORAGE) && File.read(STORAGE) == external_ip | |
DOMAINS.each do |domain| | |
LoopiaDNS.new(USERNAME, PASSWORD, domain).dyndns_ip = external_ip | |
end | |
File.open(STORAGE, 'w') {|file| file.print external_ip } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment