#!/usr/bin/ruby # # Import BIND slave configuration into MySQL db. # require 'rubygems' require 'extensions/io' require 'mysql' host='10.x.y.z' user='user' pass='pass' db='db_for_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.some.domain.com", "ns1.some.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