Capture.PNG
· 40 KiB · Image (PNG)
Brut
update-ddns.rb
· 2.0 KiB · Ruby
Brut
#!/usr/bin/env ruby
#
# Original, including IsoLinearCHiP's improvements: https://gist.github.com/IsoLinearCHiP/d6bc594143102e6acd131fc65c080f64
# Current version, adapted to Hetzner phasing out dns.h.d for console.h.d:
API="https://api.hetzner.cloud/v1"
require 'json'
require 'socket'
require 'net/http'
require 'fileutils'
require 'pstore'
FileUtils.mkdir_p "#{Dir.home}/.local/share/update-ddns"
@pstore = PStore.new "#{Dir.home}/.local/share/update-ddns/cache.pstore"
def api(path, records=nil)
cmd = if records
"curl -s -X POST -H 'Authorization: Bearer #{@token}' -H 'Content-Type: application/json' -d '{\"records\": #{records.to_json}}' #{API}#{path}"
else
"curl -s -H 'Authorization: Bearer #{@token}' #{API}#{path}"
end
json = JSON.parse `#{cmd}`
if json['error']
warn "Error: #{json['error']['message']}"
exit!
end
json
end
def cache(key, value=nil)
if value
@pstore.transaction{@pstore[key] = value}
end
@pstore.transaction(true){return @pstore[key]}
end
def current_public_ip
Socket.getifaddrs
.map{|iface| [iface.name, iface.addr.ip_address] if iface.addr.ipv4? if iface.addr}
.compact
.find{|x| x[0]=='ppp0'}[1]
end
def query
@token = cache('api_token')
zone = ARGV[0]
rrset_name = ARGV[1]
zone_id = cache('zone_id')
zone_id ||= cache('zone_id', api('/zones').find{|x| x['name'] == @zone}['id'])
public_ip = cache('current_public_ip', current_public_ip)
puts "Current WAN IP: #{public_ip}"
ips_on_record = api("/zones/#{zone_id}/rrsets/#{rrset_name}/A")['rrset']['records'].collect{|x| x['value']}
if ips_on_record.last != public_ip
api("/zones/#{zone_id}/rrsets/#{rrset_name}/A/actions/set_records", [{value: public_ip, comment: 'dynamically updated'}])
puts "record updated"
else
puts "no update necessary"
end
end
case ARGV.size
when 1
cache('api_token', ARGV[0])
puts "API token set"
when 2
(warn "Error: API token not set"; exit!) unless cache('api_token')
query
else
warn "Usage: update-ddns <zone> <record> | update-ddns <api token>"
exit!
end
| 1 | #!/usr/bin/env ruby |
| 2 | # |
| 3 | # Original, including IsoLinearCHiP's improvements: https://gist.github.com/IsoLinearCHiP/d6bc594143102e6acd131fc65c080f64 |
| 4 | # Current version, adapted to Hetzner phasing out dns.h.d for console.h.d: |
| 5 | |
| 6 | API="https://api.hetzner.cloud/v1" |
| 7 | |
| 8 | require 'json' |
| 9 | require 'socket' |
| 10 | require 'net/http' |
| 11 | require 'fileutils' |
| 12 | require 'pstore' |
| 13 | |
| 14 | FileUtils.mkdir_p "#{Dir.home}/.local/share/update-ddns" |
| 15 | @pstore = PStore.new "#{Dir.home}/.local/share/update-ddns/cache.pstore" |
| 16 | |
| 17 | def api(path, records=nil) |
| 18 | cmd = if records |
| 19 | "curl -s -X POST -H 'Authorization: Bearer #{@token}' -H 'Content-Type: application/json' -d '{\"records\": #{records.to_json}}' #{API}#{path}" |
| 20 | else |
| 21 | "curl -s -H 'Authorization: Bearer #{@token}' #{API}#{path}" |
| 22 | end |
| 23 | json = JSON.parse `#{cmd}` |
| 24 | if json['error'] |
| 25 | warn "Error: #{json['error']['message']}" |
| 26 | exit! |
| 27 | end |
| 28 | json |
| 29 | end |
| 30 | |
| 31 | def cache(key, value=nil) |
| 32 | if value |
| 33 | @pstore.transaction{@pstore[key] = value} |
| 34 | end |
| 35 | @pstore.transaction(true){return @pstore[key]} |
| 36 | end |
| 37 | |
| 38 | def current_public_ip |
| 39 | Socket.getifaddrs |
| 40 | .map{|iface| [iface.name, iface.addr.ip_address] if iface.addr.ipv4? if iface.addr} |
| 41 | .compact |
| 42 | .find{|x| x[0]=='ppp0'}[1] |
| 43 | end |
| 44 | |
| 45 | def query |
| 46 | @token = cache('api_token') |
| 47 | zone = ARGV[0] |
| 48 | rrset_name = ARGV[1] |
| 49 | zone_id = cache('zone_id') |
| 50 | zone_id ||= cache('zone_id', api('/zones').find{|x| x['name'] == @zone}['id']) |
| 51 | public_ip = cache('current_public_ip', current_public_ip) |
| 52 | puts "Current WAN IP: #{public_ip}" |
| 53 | ips_on_record = api("/zones/#{zone_id}/rrsets/#{rrset_name}/A")['rrset']['records'].collect{|x| x['value']} |
| 54 | if ips_on_record.last != public_ip |
| 55 | api("/zones/#{zone_id}/rrsets/#{rrset_name}/A/actions/set_records", [{value: public_ip, comment: 'dynamically updated'}]) |
| 56 | puts "record updated" |
| 57 | else |
| 58 | puts "no update necessary" |
| 59 | end |
| 60 | end |
| 61 | |
| 62 | case ARGV.size |
| 63 | when 1 |
| 64 | cache('api_token', ARGV[0]) |
| 65 | puts "API token set" |
| 66 | when 2 |
| 67 | (warn "Error: API token not set"; exit!) unless cache('api_token') |
| 68 | query |
| 69 | else |
| 70 | warn "Usage: update-ddns <zone> <record> | update-ddns <api token>" |
| 71 | exit! |
| 72 | end |