Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
78 lines (74 sloc) 3.22 KB
#!/usr/bin/ruby
#
# Import BIND slave configuration into MySQL db.
#
require 'rubygems'
require 'extensions/io'
require 'mysql'
host='127.0.0.1'
user='dns'
pass='dns'
db='smbind'
begin
dbh = Mysql.real_connect(host, user, pass, db)
#puts "Server version: " + dbh.get_server_info
rescue Mysql::Error => e
puts "Error code: #{e.errno}"
puts "Error message: #{e.error}"
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
ensure
for i in (`ls -1 slave/`)
config = IO.readlines( 'slave/' + i.chop )
name=config[2].split[0]
serial=config[3].split[0]
refresh=config[4].split[0]
d_retry=config[5].split[0]
expire=config[6].split[0]
ttl=config[1].split[1]
dbh.query('INSERT INTO `zones` VALUES ( "", "' + name + '", "ns0.domain.com", "ns1.domain.com","' + serial + '","' + refresh + '", "' + d_retry + '", "' + expire + '", "' + ttl + '", "yes", "1", "yes")')
res = dbh.query('SELECT `id` FROM `zones` WHERE name="' + name + '"')
zone=nil
res.each_hash do |row|
zone=row["id"]
end
subdomain = nil
config.each_index do |line|
if config[line].match('.*?ORIGIN')
subdomain=config[line].split[1].chop
end
if config[line].match('\s.A\s')
if config[line].split[0].match('A')
dbh.query('INSERT INTO `records` VALUES (\'NULL\', \'' + zone + '\', \'@\', \'A\', \'0\', \'' + config[line].split[1] + '\', \'yes\')')
else
dbh.query('INSERT INTO `records` VALUES (\'NULL\', \'' + zone + '\', \'' + config[line].split[0] + '.' + subdomain + '\', \'A\', \'0\', \'' + config[line].split[2] + '\', \'yes\')')
end
end
if config[line].match('\s.*?MX')
if config[line].split[2].chop.count(".").to_s=="0"
dbh.query('INSERT INTO `records` VALUES (\'NULL\', \'' + zone + '\', \'@\', \'MX\', \'' + config[line].split[1] +'\', \'' + config[line].split[2] + '.' + subdomain + '\', \'yes\')')
else
dbh.query('INSERT INTO `records` VALUES (\'NULL\', \'' + zone + '\', \'@\', \'MX\', \'' + config[line].split[1] +'\', \'' + config[line].split[2].chop + '\', \'yes\')')
end
end
if config[line].match('\s.*?CNAME')
if config[line].split[2].chop.count(".").to_s=="0"
dbh.query('INSERT INTO `records` VALUES (\'NULL\', \'' + zone + '\', \'' + config[line].split[0] + '.' + subdomain + '\', \'CNAME\', \'0\', \'' + config[line].split[2] + '.' + subdomain + '\', \'yes\')')
else
dbh.query('INSERT INTO `records` VALUES (\'NULL\', \'' + zone + '\', \'' + config[line].split[0] + '.' + subdomain + '\', \'CNAME\', \'0\', \'' + config[line].split[2].chop + '\', \'yes\')')
end
end
if config[line].match('\s.*?PTR')
if config[line].split[0].match('PTR')
dbh.query('INSERT INTO `records` VALUES (\'NULL\', \'' + zone + '\', \'@\', \'PTR\', \'0\', \'' + config[line].split[1].chop + '\', \'yes\')')
else
if config[line].split[2].chop.count(".").to_s=="0"
dbh.query('INSERT INTO `records` VALUES (\'NULL\', \'' + zone + '\', \'' + config[line].split[0] + '.' + subdomain + '\', \'PTR\', \'0\', \'' + config[line].split[2].chop + '.' + subdomain + '\', \'yes\')')
else
dbh.query('INSERT INTO `records` VALUES (\'NULL\', \'' + zone + '\', \'' + config[line].split[0] + '.' + subdomain + '\', \'PTR\', \'0\', \'' + config[line].split[2].chop + '\', \'yes\')')
end
end
end
end
end
dbh.close if dbh
end