Last active
July 12, 2024 06:10
-
-
Save ayn/fa156eea5b6f29fe86be255369d6f529 to your computer and use it in GitHub Desktop.
update dnsmadeeasy dualstack IPs
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
| ruby-3.3.4 |
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
| [Unit] | |
| Description=DNS Made Easy dualstack IP update | |
| After=network.target | |
| [Service] | |
| WorkingDirectory=/home/ayn/work/dme-dualstack-update | |
| Environment=GEM_HOME=/home/ayn/.gem/ruby/3.3.4 | |
| Environment=PATH=/home/ayn/.gem/ruby/3.3.4/bin:$PATH | |
| ExecStart=/home/ayn/.rubies/ruby-3.3.4/bin/bundle exec update-ip.rb | |
| Restart=on-failure | |
| RestartSec=5 | |
| [Install] | |
| WantedBy=multi-user.target |
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
| apiKey: my-dme-api-key | |
| secretKey: my-dme-secret-key | |
| domainName: my-domain.org | |
| recordName: my-dynamic-hostname |
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
| # frozen_string_literal: true | |
| source "https://rubygems.org" | |
| gem 'dnsmadeeasy' | |
| gem 'pry' |
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 'dnsmadeeasy' | |
| require 'net/http' | |
| require 'json' | |
| yml = YAML.load_file('dnsmadeeasy.yml') | |
| api_key = yml['apiKey'] | |
| secret_key = yml['secretKey'] | |
| domain_name = yml['domainName'] | |
| record_name = yml['recordName'] | |
| DME = DnsMadeEasy::Api::Client.new(api_key, secret_key) | |
| def get_public_ip(version = 4) | |
| uri = URI("https://api#{version == 6 ? '6' : ''}.ipify.org?format=json") | |
| response = Net::HTTP.get(uri) | |
| JSON.parse(response)['ip'] | |
| end | |
| def update_dns_record(domain_name, record_name, ip_address, record_type) | |
| record_id = DME.find_record_ids(domain_name, record_name, record_type).first | |
| DME.update_record(domain_name, record_id, record_name, record_type, ip_address, { 'ttl' => '300' }) | |
| rescue | |
| puts "Error updating ip for #{record_name} with type #{record_type}, id #{record_id}, ip #{ip_address}: #{$!}" | |
| else | |
| puts "Successfully updated #{record_type} record for #{record_name} to #{ip_address}" | |
| end | |
| def update_dns(domain_name, record_name, ip4, ip6) | |
| update_dns_record(domain_name, record_name, ip4, 'A') | |
| update_dns_record(domain_name, record_name, ip6, 'AAAA') | |
| File.write('last_ip4.txt', ip4) | |
| File.write('last_ip6.txt', ip6) | |
| end | |
| def ip_changed?(current_ip4, current_ip6) | |
| last_ip4 = File.read('last_ip4.txt').strip rescue nil | |
| last_ip6 = File.read('last_ip6.txt').strip rescue nil | |
| current_ip4 != last_ip4 || last_ip6 != current_ip6 | |
| end | |
| loop do | |
| current_ip4 = get_public_ip(4) | |
| current_ip6 = get_public_ip(6) | |
| if ip_changed?(current_ip4, current_ip6) | |
| update_dns(domain_name, record_name, current_ip4, current_ip6) | |
| else | |
| puts "IP addresses unchanged" | |
| end | |
| sleep 300 | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment