实现对dns A记录交互式进行更改、增加及删除的shell脚本,感兴趣的朋友可以参考下,此外正在学习shell脚本编程的朋友,可以作为提高水平的一个案例哦。
写这个时因为绝对路径的问题老是出错,注意下边蓝色字体部分。
#!/bin/bash
#written by booduklee
#2011-05-08
#this program is used to change the A_record
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
export PATH
echo "--------------welcome to change your A_record---------"
# whether the user is root
if echo $USER | grep '\<root\>' >> /dev/null;
then
echo "permmision passed"
else
exit 3
fi
# whether the scripts if running now
if [ -f key_lock ];
then
echo "it has runned by others; please wait"
exit 2
else
echo $$ > key_lock
fi
#define path
config=/var/named/chroot/var/named
export config
# create function --------------
function change_A {
read -p "please input which zone you want to change:" zone;
if ls $config | grep $zone.zone;
then
read -p "please input which A_record you want to change:" old_a
if grep ^$old_a $config/$zone.zone; then
read -p "please input the new A_record:" new_a
sed -i "s/^$old_a/$new_a/" $config/$zone.zone
else
echo "this A_record not find"
fi
else
echo "this zone not find"
fi
}
function add_A {
read -p "please input which zone you want to add A_record:" zone;
if ls $config | grep $zone;
then
read -p "please input your new record:" n_a;
read -p "plese input your ip_address:" ip;
cat >> $config/$zone.zone << EOF
$n_a IN A $ip
EOF
else
echo "this zone not find"
fi
}
function del_A {
read -p "please input which zone you want to delete A_record:" zone;
if ls $config | grep $zone;
then
read -p "please input which A_record you want to del:" d_a;
if grep ^$d_a $config/$zone.zone; then
sed -i "/^$d_a/d" $config/$zone.zone
else
echo "this A_record not find"
fi
else
echo "this zone not find"
fi
}
#--------------------apply funciton----------
while true;
do
read -p "please input your choice:(add|change|del|quit)" choice
case $choice in
add)
add_A;
;;
change)
change_A;
;;
del)
del_A;
;;
*)
rm -rf key_lock
exit 3;
;;
esac
done
rm -rf key_lock
#-----------------it's over------------------------