Skip to content

Instantly share code, notes, and snippets.

@jiphex
Last active February 21, 2017 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiphex/76489c37cbdd9888110583a8233dfd9b to your computer and use it in GitHub Desktop.
Save jiphex/76489c37cbdd9888110583a8233dfd9b to your computer and use it in GitHub Desktop.
123reg to TinyDNS converter
require 'ipaddr'
require 'ostruct'
require 'json'
require 'pp'
data = JSON.load(STDIN)["json"]
ZONE = data['dns']['zone']
STDERR.puts "DNS 123REG BS for [#{ZONE}]"
exit if ZONE.nil?
def ttl(rec)
if rec.record_ttl.nil?
""
else
":#{rec.record_ttl.to_s}"
end
end
def host(rec)
if rec.host == "@" or rec.host.nil? or rec.host == 0 then
ZONE
else
rec.host+"."+ZONE
end
end
def data(rec)
rec.data.gsub(/\:/,"\072").gsub(/\.$/,"")
end
data["dns"]["records"].each do |rec|
STDERR.puts rec
rec = OpenStruct.new(rec)
case rec.type
when "CNAME"
puts "C#{host(rec)}:#{data(rec)}#{ttl(rec)}"
when "A"
puts "+#{host(rec)}:#{data(rec)}#{ttl(rec)}"
when "TXT"
puts "'#{host(rec)}:#{data(rec)}#{ttl(rec)}"
when "AAAA"
tdrec = IPAddr.new(rec.data).ip6_arpa.reverse[9..-1].gsub(/\./,'')
puts "3#{host(rec)}:#{tdrec}#{ttl(rec)}"
when "MX"
puts "@#{host(rec)}::#{data(rec)}:#{rec.mx_priority}#{ttl(rec)}"
when "SOA"
next
when "NS"
next
else
STDERR.puts "WRITE A CONVERTER FOR RECORD TYPE #{rec.type}: #{rec}"
end
end

THIS MAY BE BROKEN, BE CAREFUL AND CHECK EVERYTHING MANUALLY

123-reg unhelpfully don't provide any way of exporting DNS data from their control panel which is a massive pain when you want to move records away from their crappy DNS service to something better.

Luckily, if you open the "Advanced DNS" editor panel, it uses an XMLHTTPRequest to download the records in JSON format. You can retrieve this data by navigating to the page with your browser's web inspector open, then find the request to the "get_dns" page and extract the body of the response.

If you put that JSON data into the STDIN of this ruby script, it will return the records in TinyDNS format on STDOUT and debugging on STDERR

Caveats

  • It only supports accepting one zone on STDIN
  • Only the listed record types are supported, but it should be easy to add others
  • NS/SOA records are skipped, you will need to add these yourself
  • Only colons are escaped, other characters may cause issues
  • Record lengths aren't checked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment