No products in the cart.
Learn how to format DNS domain resolution results using a Shell script. Follow our guide for efficient DNS data management and analysis.
享一个Shell脚本。大家不一定用的上,但可以参考;再一个是可以通过下列需求进行练手,初学者可以试试!
脚本还有优化的地方(懒得改了hh)有兴趣得朋友可以简化一下下列脚本。
需求如下:
脚本需要实现下列三个功能,分别是添加、删除和修改。对应下列就是格式化前后的效果。
1) 添加dns解析
格式化前:openapi-test.qiuyl.com 192.168.200.10
格式化后:dnscmd ./RecordAdd qiuyl.com openapi-test A 192.168.200.10
2) 修改dns解析 openapi-test.qiuyl.com (修改需要先删除解析,避免有两条记录)
格式化后:echo y| dnscmd ./RecordDelete qiuyl.com openapi-test A 192.168.200.10
格式化后:dnscmd ./RecordAdd qiuyl.com openapi-test A 192.168.200.11
3) 删除dns解析 openapi-test.qiuyl.com 192.168.200.11
格式化后:echo y| dnscmd ./RecordDelete qiuyl.com openapi-test A 192.168.200.11
1)目录结构
[root@cd0215 dns_jiexi]# ll
total 4
drwxr-xr-x 2 root root 66 Aug 6 17:53 logs # 存放日志目录
drwxr-xr-x 2 root root 92 Aug 7 17:53 sources # 存放数据源目录
-rw-r--r-- 1 root root 2836 Aug 7 17:48 start.sh
# 源文件格式参考(需严格按照下列格式)
[root@cd0215 dns_jiexi]# cat ./sources/dns-add.txt
openapi-test-1.qiuyl.com 192.168.200.10
openapi-test-2.qiuyl.com 192.168.200.11
2)使用说明
[root@cd0215 dns_jiexi]# sh start.sh
请输入一个命令参数:[ add | del | update ]
3)脚本源码
#!/bin/bash
# 文件名称定义
FILE_ADD="./sources/dns-add.txt"
FILE_DEL="./sources/dns-del.txt"
FILE_UPDATE="./sources/dns-update.txt"
FILE_UPDATE_DEL="./sources/dns-update-del.txt"
FILE_ADD_LOG="./logs/dns-add.log"
FILE_DEL_LOG="./logs/dns-del.log"
FILE_UPDATE_LOG="./logs/dns-update.log"
# 数据读取 函数
readDate_func(){
FILE="$1"
# 读取并处理每一行
while IFS=' ' read -r DNS_FULL_NAME IP; do
# 提取域名
DNS_FIREST_NAME=$(echo "$DNS_FULL_NAME" | awk -F. '{print $2"."$3}')
DNS_SECOND_NAME=$(echo "$DNS_FULL_NAME" | awk -F. '{print $1}')
DNS_IP=$IP
if [ $FILE == $FILE_ADD ];then
date_func
echo -e "$TIME dnscmd . \e[96m/RecordAdd\e[0m \e[92m$DNS_FIREST_NAME $DNS_SECOND_NAME A $DNS_IP \e[0m" >> $FILE_ADD_LOG
elif [ $1 == $FILE_DEL ];then
date_func
echo -e "$TIME echo y | dnscmd . \e[96mRecordDelete\e[0m \e[92m$DNS_FIREST_NAME $DNS_SECOND_NAME A $DNS_IP \e[0m" >> $FILE_DEL_LOG
elif [ $1 == $FILE_UPDATE_DEL ];then
date_func
echo -e "$TIME echo y | dnscmd . \e[96mRecordDelete\e[0m \e[92m$DNS_FIREST_NAME $DNS_SECOND_NAME A $DNS_IP \e[0m" >> $FILE_UPDATE_LOG
elif [ $1 == $FILE_UPDATE ];then
date_func
echo -e "$TIME dnscmd . \e[96m/RecordAdd\e[0m \e[92m$DNS_FIREST_NAME $DNS_SECOND_NAME A $DNS_IP \e[0m" >> $FILE_UPDATE_LOG
fi
done < "$FILE"
}
date_func(){
TIME=`date '+%Y-%m-%d %H:%M:%S'`
}
add_func(){
readDate_func $FILE_ADD
}
del_func(){
readDate_func $FILE_DEL
}
update_func(){
readDate_func $FILE_UPDATE_DEL
readDate_func $FILE_UPDATE
}
# 判断用户对应操作
if [ -z "$1" ];then
echo -e "请输入一个命令参数:\e[92m[ add | del | update ]\e[0m"
exit 6
fi
if [ $1 == "add" ];then
if [ -f "$FILE_ADD" ];then
add_func
if [ $? == "0" ];then
echo -e "add command execution \e[92msuccessful!\e[0m"
echo "logs dir: $FILE_ADD_LOG"
fi
else
echo -e "$FILE_ADD \e[93m文件不存在,请创建!\e[0m"
exit 1
fi
elif [ $1 == "del" ];then
if [ -f "$FILE_DEL" ];then
del_func
if [ $? == "0" ];then
echo -e "del command execution \e[92msuccessful!\e[0m"
echo "logs dir: $FILE_DEL_LOG"
fi
else
echo -e "$FILE_DEL \e[93m文件不存在,请创建!\e[0m"
exit 2
fi
elif [ $1 == "update" ];then
if [ -f "$FILE_UPDATE_DEL" ];then
echo -n ""
else
echo -e "$FILE_UPDATE_DEL \e[93m文件不存在,请创建!\e[0m"
exit 4
fi
if [ -f "$FILE_UPDATE" ];then
update_func
if [ $? == "0" ];then
echo -e "update command execution \e[92msuccessful!\e[0m"
echo "logs dir: $FILE_UPDATE_LOG"
fi
else
echo -e "$FILE_UPDATE \e[93m文件不存在,请创建!\e[0m"
exit 5
fi
else
echo -e "请输入正确的操作指令:\e[92m[ add | del | update ]\e[0m"
fi