Bind9 DNS 区域传输脚本 - Bash 脚本以避免重复

发布于 2024-09-24 07:37:59 字数 1497 浏览 7 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴随着你 2024-10-01 07:37:59

一个问题可能是您的 wget 会创建一个文件,无论是否存在源文件,因此检查是否存在始终为真。

if [ -s $TMPNAMED ]
then
    echo "done."    # file exists AND has data
else
    echo "no new data!" 
    exit
fi

将测试它是否为空或不存在,如果是则退出。这也可能是您的 if [ -e $ZONELOCATION/$ZONE.db ] 的问题。

sedawk 可以在一行中完成所有这些操作:

grep "^zone" $TMPNAMED | cut -d " " -f "2" | cut -d "\"" -f 2 > $TMPZONEFILE
sed '1,5d' $TMPZONEFILE > $TMP

但我需要查看一些示例数据才能提供解决方案。

简化引用:

echo "done. ($TMPZONEFILE)"

您没有使用 IGNORE 变量或 updates 变量。我看不出有什么理由导出它。另外,如果您在其他地方依赖它,一旦 while 循环退出,它的值将不会保留,因为将某些内容(在本例中为 grep)传输到 while< /code> 设置一个子 shell。最好执行以下操作之一:

Bash:

while ...
do
    ...
done <(grep -vf ignore.txt $TMPZONEFILE)

sh:

grep -vf ignore.txt $TMPZONEFILE > tmp.out
while ...
do
    ...
done < tmp.out

顺便说一句,我建议使用 mktemptempfile 来创建临时文件。

这可能更具可读性,并且允许您包含引号而不必转义它们:

cat << EOF >> "$NAMED"
zone "$ZONE" {
  type slave;
  file "$ZONELOCATION/$ZONE.db";
  masters { 91.121.75.205; };
  allow-notify { 91.121.75.205; };
  };
EOF

引用包含文件名的变量始终是一个好习惯。

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.

if [ -s $TMPNAMED ]
then
    echo "done."    # file exists AND has data
else
    echo "no new data!" 
    exit
fi

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 or awk could do all of this in one line:

grep "^zone" $TMPNAMED | cut -d " " -f "2" | cut -d "\"" -f 2 > $TMPZONEFILE
sed '1,5d' $TMPZONEFILE > $TMP

but I would need to see some sample data to offer a solution.

Simplified quoting:

echo "done. ($TMPZONEFILE)"

You're not using the IGNORE variable or the updates variable. I don't see any reason to export it. Also, if you are relying on it elsewhere, its value won't survive once the while loop exits since piping something (grep in this case) into while sets up a subshell. It may be better to do one of these:

Bash:

while ...
do
    ...
done <(grep -vf ignore.txt $TMPZONEFILE)

sh:

grep -vf ignore.txt $TMPZONEFILE > tmp.out
while ...
do
    ...
done < tmp.out

I recommend using mktemp or tempfile to create temporary files, by the way.

This might be more readable and allows you to include quotes without having to escape them:

cat << EOF >> "$NAMED"
zone "$ZONE" {
  type slave;
  file "$ZONELOCATION/$ZONE.db";
  masters { 91.121.75.205; };
  allow-notify { 91.121.75.205; };
  };
EOF

It's always a good habit to quote variables that contain filenames.

雪化雨蝶 2024-10-01 07:37:59

如果您要费尽心思来同步 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文