上次的博文簡單使用了BIND的視圖功能實作了簡單的智能dns,此篇博文結合了mysql實作真正意義上的智能DNS系統
需要準備東西
首先yum安裝mysql。。。
yum install mysql mysql-server mysql-devel
下載下傳bind主程式源碼:
ftp://ftp.isc.org/isc/bind9/9.8.5-P1/bind-9.8.5-P1.tar.gz
下載下傳bind-sdb驅動:
http://nchc.dl.sourceforge.net/project/mysql-bind/mysql-bind/mysql-bind-0.2%20src/mysql-bind.tar.gz
解壓:
tar xfbind-9.8.5-P1.tar.gz
tar xfmysql-bind.tar.gz
編譯安裝bind
将mysql-sdb的更新檔檔案拷貝到bind安裝源檔案目錄下
cp mysql-bind/mysqldb.c bind-9.8.5-P1/bin/named/
cp mysql-bind/mysqldb.h bind-9.8.5-P1/bin/named/
<a></a>
DBDRIVER_OBJS= mysqldb.@O@
DBDRIVER_SRCS= mysqldb.c
DBDRIVER_INCLUDES= -I'/usr/include/mysql'
修改bind源碼目錄下的bin/named/main.c,添加以下幾行
/*#include "xxdb.h" */
後面添加
#include "mysqldb.h"
/*xxdb_init();*/
在後面添加
mysqldb_init();
再找到
/*xxdb_clear();*/
mysqldb_clear();
修改bin/named/mysqldb.c
将
#include <named/mysqldb.h>
修改為
#include <bin/named/mysqldb.h>
編譯安裝
./configure--prefix=/usr --sysconfdir=/etc
make&& make install
下載下傳電信和聯通的位址清單ACL,存放在/etc/named目錄下
http://www.centos.bz/wp-content/uploads/2012/02/CHINANET.acl
http://www.centos.bz/wp-content/uploads/2012/02/CNC.acl
在/etc/named.conf添加2條,進而将電信和聯通的ACL位址清單包含進來
include"/etc/named/CHINANET.acl"
include"/etc/named/CNC.acl"
然後在主配置檔案中定義2個視圖:
CNC是聯通的視圖 ,CHINANET是電信的視圖
view CNC{
match-clients { CNC; };
recursion no;
zone "." IN {
type hint;
file "named.ca";
};
zone "localhost" IN {
type master;
file "named.localhost";
zone "0.0.127.in-addr.arpa" IN {
file "named.loopback";
zone "lustlost.com" IN {
notify no;
database "mysqldb smartdns CNClocalhost root 123456"; #這裡定義的資料庫相關的配置,字段分别為:SQLtype DataBase Table Host User Password
};
view CHINANET {
match-clients { CHINANET; };
};
database "mysqldb smartdnsCHINANET localhost root 123456";
然後建立資料庫:
create database smartdns;
use smartdns;
建立聯通表
CREATE TABLE CNC(name varchar(255) default NULL,ttl int(11) default NULL,rdtypevarchar(255) default NULL,rdata varchar(255) default NULL) TYPE=MyISAM;
插入SOA其實授權資訊
INSERTINTO CNC VALUES('lustlost.com',259200,'SOA','lustlost.com.www.lustlost.com.20130617 28800 720086400 28800');
插入NS記錄(實驗環境,位址皆為内網IP)
INSERT INTO CNC VALUES ('lustlost.com', 259200, 'NS','ns1.lustlost.com.'),('lustlost.com', 259200, 'NS', 'ns2.lustlost.com.'),('lustlost.com', 259200, 'MX', '10 mail.lustlost.com.'),('ns1.lustlost.com',259200, 'A', '192.168.1.1'),('ns2.lustlost.com', 259200, 'A','192.168.1.2'),('mail.lustlost.com', 259200, 'A','192.168.1.3'),('www.lustlost.com', 259200, 'A', '192.168.1.4');
建立電信表
CREATE TABLE CHINANET (name varchar(255) default NULL,ttl int(11) default NULL,rdtypevarchar(255) default NULL,rdata varchar(255) default NULL) TYPE=MyISAM;
INSERT INTO CHINANET VALUES ('lustlost.com',259200,'SOA','lustlost.com.www.lustlost.com.2013061728800 7200 86400 28800');
INSERT INTO CHINANET VALUES ('lustlost.com', 259200, 'NS','ns1.lustlost.com.'),('lustlost.com', 259200, 'NS', 'ns2.lustlost.com.'),('lustlost.com', 259200, 'MX', '10 mail.lustlost.com.'),('ns1.lustlost.com',259200, 'A', '192.168.2.1'),('ns2.lustlost.com', 259200, 'A', '192.168.2.2'),('mail.lustlost.com',259200, 'A', '192.168.2.3'),('www.lustlost.com', 259200, 'A', '192.168.2.4');
完畢....
本文轉自lustlost 51CTO部落格,原文連結:http://blog.51cto.com/lustlost/1227582,如需轉載請自行聯系原作者