Bind9 DNS 区域传输脚本 - Bash 脚本以避免重复
我创建了一个 bash 脚本来在主 DNS 服务器和辅助 DNS 服务器之间传输区域。 它从主区域下载我的区域列表并检查是否有任何新区域,然后下载这些区域文件并将其插入到区域目录和 .local 文件中以进行绑定。
我遇到的问题是,如果区域文件不存在,脚本会将详细信息输入到 .local 中,无论此配置是否已存在。
有人可以帮助我区分已经存在的区域并简单地下载区域文件。 我已将我的脚本粘贴在下面,如果有人对其工作原理有任何疑问,请随时询问。 (有人可以包装一下代码吗,在我尝试的任何浏览器中它都无法正常工作!)
#!/bin/sh
NAMED="/etc/bind/named.conf.local"
TMPNAMED="/tmp/zns-441245.temp"
TMPZONEFILE="/tmp/zones.txt"
TMP="/tmp/zns-732.temp"
ZONELOCATION="/var/cache/bind"
IGNORE=`cat ignore.txt`
logger DNS Update script running...
echo -n "Checking for new named.conf... "
wget -q http://91.121.75.205:10801/named/named.conf -O $TMPNAMED
if [ -e $TMPNAMED ]
then
echo "done."
else
echo "no new data!"
exit
fi
echo -n "Generating zone names... "
grep "^zone" $TMPNAMED | cut -d " " -f "2" | cut -d "\"" -f 2 > $TMPZONEFILE
sed '1,5d' $TMPZONEFILE > $TMP
mv $TMP $TMPZONEFILE
echo "done. ("$TMPZONEFILE")"
echo "Generating zone info... "
grep -vf ignore.txt $TMPZONEFILE | while read ZONE; do
echo -n "Checking for $ZONELOCATION/$ZONE.db "
if [ -e $ZONELOCATION/$ZONE.db ]
then
echo "[ exists ]"
else
export updates="yes"
echo "[ doesn't exist ]"
echo "New zone available ($ZONE)... "
echo "zone \"$ZONE\" {
type slave;
file \"$ZONELOCATION/$ZONE.db\";
masters { 91.121.75.205; };
allow-notify { 91.121.75.205; };
};" >> $NAMED
fi
done
echo "Updating Bind configuration... "
/etc/init.d/bind9 restart
rm $TMPZONEFILE
rm $TMPNAMED
I created a bash script to transfer my zones between my primary and secondary DNS server.
It downloads my zone list from the primary and checks for any new zones and then downloads and inserts those zone files into the zone directory and into the .local file for bind.
The problem I have is that if the zone file does not exist, the script will enter the details into the .local regardless of if this config already exists or not.
Can someone help me out to distinguish between zones that already exist and simply download the zone file.
I have pasted my script below and if anyone has any queries on how it works, please feel free to ask.
(can someone wrap the code please, it never works properly for me in any browser I try!)
#!/bin/sh
NAMED="/etc/bind/named.conf.local"
TMPNAMED="/tmp/zns-441245.temp"
TMPZONEFILE="/tmp/zones.txt"
TMP="/tmp/zns-732.temp"
ZONELOCATION="/var/cache/bind"
IGNORE=`cat ignore.txt`
logger DNS Update script running...
echo -n "Checking for new named.conf... "
wget -q http://91.121.75.205:10801/named/named.conf -O $TMPNAMED
if [ -e $TMPNAMED ]
then
echo "done."
else
echo "no new data!"
exit
fi
echo -n "Generating zone names... "
grep "^zone" $TMPNAMED | cut -d " " -f "2" | cut -d "\"" -f 2 > $TMPZONEFILE
sed '1,5d' $TMPZONEFILE > $TMP
mv $TMP $TMPZONEFILE
echo "done. ("$TMPZONEFILE")"
echo "Generating zone info... "
grep -vf ignore.txt $TMPZONEFILE | while read ZONE; do
echo -n "Checking for $ZONELOCATION/$ZONE.db "
if [ -e $ZONELOCATION/$ZONE.db ]
then
echo "[ exists ]"
else
export updates="yes"
echo "[ doesn't exist ]"
echo "New zone available ($ZONE)... "
echo "zone \"$ZONE\" {
type slave;
file \"$ZONELOCATION/$ZONE.db\";
masters { 91.121.75.205; };
allow-notify { 91.121.75.205; };
};" >> $NAMED
fi
done
echo "Updating Bind configuration... "
/etc/init.d/bind9 restart
rm $TMPZONEFILE
rm $TMPNAMED
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个问题可能是您的
wget
会创建一个文件,无论是否存在源文件,因此检查是否存在始终为真。将测试它是否为空或不存在,如果是则退出。这也可能是您的
if [ -e $ZONELOCATION/$ZONE.db ]
的问题。sed
或awk
可以在一行中完成所有这些操作:但我需要查看一些示例数据才能提供解决方案。
简化引用:
您没有使用
IGNORE
变量或updates
变量。我看不出有什么理由导出它。另外,如果您在其他地方依赖它,一旦while
循环退出,它的值将不会保留,因为将某些内容(在本例中为grep
)传输到while< /code> 设置一个子 shell。最好执行以下操作之一:
Bash:
sh:
顺便说一句,我建议使用
mktemp
或tempfile
来创建临时文件。这可能更具可读性,并且允许您包含引号而不必转义它们:
引用包含文件名的变量始终是一个好习惯。
One problem may be that your
wget
creates a file regardless of whether there's a source file so checking for existence will always be true.will test to see if it's empty or non-existent and exit if so. This may be an issue with your
if [ -e $ZONELOCATION/$ZONE.db ]
as well.sed
orawk
could do all of this in one line:but I would need to see some sample data to offer a solution.
Simplified quoting:
You're not using the
IGNORE
variable or theupdates
variable. I don't see any reason to export it. Also, if you are relying on it elsewhere, its value won't survive once thewhile
loop exits since piping something (grep
in this case) intowhile
sets up a subshell. It may be better to do one of these:Bash:
sh:
I recommend using
mktemp
ortempfile
to create temporary files, by the way.This might be more readable and allows you to include quotes without having to escape them:
It's always a good habit to quote variables that contain filenames.
如果您要费尽心思来同步
named.conf
,您不妨 rsync 整个配置包括区域文件,而不必费心在之间使用区域传输主要和次要。使用 AXFR 来连接从属服务器并不是强制性的。如果您对一个区域的所有服务器都有管理控制权,那么将它们全部视为主服务器是完全可以接受的。
If you're going to all of that trouble to synchronise
named.conf
you might just as well rsync the whole config including the zone files, and not bother using zone transfers between primary and secondary.It's by no means mandatory to use AXFR to slave servers. If you've got administrative control over all of the servers for a zone it's quite acceptable to treat them all as masters.