脚本产生背景:手上有很多域名,分布在多家注册商,经常到期提醒不到位,或不可靠,人工查询到期时间又容易忽略,所以编写此shell脚本提供可靠的自动续费通知。
脚本使用:
脚本可以创建为/root/sh/domain_check.sh
添加自动计划任务
安装whois或jwhois
受检查域名放在/root/sh/domain_list.txt,一行一个
检查结果会在ssh终端打印,也可以选择增加邮件提醒
脚本内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
#!/bin/bash ################################### # author mail@zhaoyanan.cn # function 域名续费提醒 # # create: touch /root/sh/domain_check.sh; chmod 700 /root/sh/domain_check.sh # 被检查域名一行一个,放/root/sh/domain_list.txt文件中 # 简单邮件通知需要25端口通信,高级邮件通知支持中文,需要mutt支持 # cron: # 05 15 10 * * root /root/sh/domain_check.sh do >> /root/sh/domain_check.log 2>&1 # # 参考:https://www.cyberciti.biz/tips/domain-check-script.html/amp # # Change History: # date author note # 2017/08/31-09/01 mail@zhaoyanan.cn 基于原作者部分思路,改写 # 2017/09/04 mail@zhaoyanan.cn 格式化输出格式 # 2017/09/05 mail@zhaoyanan.cn 增加文本输出;调整whois服务器,增加cn查询;增加单域名查询超时;简化输出格式,避免格式混乱;增加检测域名列表文件和结果文件的格式双向转换 ################################### ############# ENV ################# # 脚本名称,临时文件或邮件中可能用到,避免同类脚本的变量的冲突。下面直接取脚本名 export project_name=`echo ${0##*/} | cut -d’.’ -f 1` # 工作目录,可能会产生临时文件 export work_dir=/root/sh # 临时文件目录 export tmp_dir=/tmp # 受检测域名列表的文件 export check_domain_list=/root/sh/domain_list.txt # 单个域名查询超时时间,可以是10s,即10秒,也可以是1.2m,即1.2分钟 export max_time=10s # 到期剩余天数设定,在此范围内的域名会单独列出来 export expir_days=200 # 邮件配置,默认发件人 export mailfromadd=“no-reply@zhaoyanan.cn” # 邮件配置,默认收件人 export mailtoadd=“mail@zhaoyanan.cn” # 发邮件方式,没有装mutt的就填mailto_simple,装了mutt是的可以填mailto_advanced,后者支持中文,被判断为垃圾邮件的概率也较小 # 值为no,就是关闭发邮件 #export mailto=mailto_simple #export mailto=mailto_advanced export mailto=no # source function library #. /etc/rc.d/init.d/functions # 时间和环境变量,一般无需修改 export datetime=$(date +%Y%m%d–%H%M) export unixtime_now=$(date +%s) export HOME=/root export LANG=C #export LC_ALL=C export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin export parameter1=$1 ############# PROC ################# # 记录开始时间 start_time=$(date +%s) ### 函数 ### # 帮助信息 function help_msg() { echo –e “\033[41merror!!!\033[0m” cat <<EOF 域名续费提醒脚本 HELP: 在计划任务中使用,或手动执行 exec eg: sh $0 do 结果会保存在:${work_dir}/domain_check_result.txt 更多参数见脚本内容开始部分 提醒:请保持ssh终端窗口足够大,避免结果显示混乱 EOF echo –e “\033[41merror!!! 请查看上面的帮助信息\033[0m” } # 简单发件函数 function mailto_simple() { # mail /usr/sbin/sendmail –t <<EOF From: $mailfromadd To: $mailtoadd Cc: $mailccadd Subject: [${project_name}] $sub $msg EOF } # 标准发件函数 function mailto_advanced() { # mail echo “$msg” | /opt/mutt/bin/mutt \ –e ‘set content_type=”text/html”‘ \ –s “[${project_name}] $sub” \ –e ‘my_hdr from:’“$mailfromadd” \ –c “$mailccadd” \ “$mailtoadd” — } function check_domain() { check_domain_num=`wc –l $check_domain_list | awk ‘{ print $1; }’` if [ “$check_domain_num” –eq 0 ];then echo “受检测域名列表文件${check_domain_list}不存在或者为空” exit 1 else echo “确保${check_domain_list}为unix格式,执行转换” dos2unix $check_domain_list domain_array=($(cat ${check_domain_list})) fi cat /dev/null > ${tmp_dir}/domain_list_result_succ_tmp_${project_name}.txt cat /dev/null > ${tmp_dir}/domain_list_result_expire_tmp_${project_name}.txt cat /dev/null > ${tmp_dir}/domain_list_result_succ_${project_name}.txt cat /dev/null > ${tmp_dir}/domain_list_result_expire_${project_name}.txt cat /dev/null > ${tmp_dir}/domain_list_result_failure_${project_name}.txt echo “检查开始…” for domain in ${domain_array[*]} do echo “检查${domain}” # 首次检查 domain_suffix=`echo ${domain} | awk –F‘.’ ‘{ print $(NF-0); }’` if [ “$domain_suffix” == “cn” ];then timeout $max_time whois –h whois.cnnic.cn –f –d $domain > ${tmp_dir}/whois_info_${project_name}.txt domain_expir_date=`cat ${tmp_dir}/whois_info_${project_name}.txt | grep “Expir” | head –n 1 | awk –F: ‘{ print $2; }’ | sed ‘s/^[ \t]*//g’ | awk –F‘[T ]’ ‘{ print $1; }’` domain_registrar=`cat ${tmp_dir}/whois_info_${project_name}.txt | grep “Registrar:” | awk –F‘: ‘ ‘{ print $2; }’` else timeout $max_time whois –f –d $domain > ${tmp_dir}/whois_info_${project_name}.txt domain_expir_date=`cat ${tmp_dir}/whois_info_${project_name}.txt | grep “Expir” | head –n 1 | awk –F: ‘{ print $2; }’ | sed ‘s/^[ \t]*//g’ | awk –F‘[T ]’ ‘{ print $1; }’` domain_registrar=`cat ${tmp_dir}/whois_info_${project_name}.txt | grep “Registrar:” | awk –F‘: ‘ ‘{ print $2; }’` fi # 第2次检查 if [ “$domain_expir_date” == “” ];then echo “没有查询到${domain},重查一遍。可能是因为网络问题,也可能是查询频繁,被whois服务器暂时封了” sleep 5 timeout $max_time whois –f –d $domain > ${tmp_dir}/whois_info_${project_name}.txt domain_expir_date=`cat ${tmp_dir}/whois_info_${project_name}.txt | grep “Expir” | head –n 1 | awk –F: ‘{ print $2; }’ | sed ‘s/^[ \t]*//g’ | awk –F‘[T ]’ ‘{ print $1; }’` domain_registrar=`cat ${tmp_dir}/whois_info_${project_name}.txt | grep “Registrar:” | awk –F‘: ‘ ‘{ print $2; }’` fi # 第3次检查 if [ “$domain_expir_date” == “” ];then echo “仍没有查询到${domain},换个whios服务器,再重查一遍” timeout $max_time whois –h whois.name.com –f –d $domain > ${tmp_dir}/whois_info_${project_name}.txt domain_expir_date=`cat ${tmp_dir}/whois_info_${project_name}.txt | grep “Expir” | head –n 1 | awk –F: ‘{ print $2; }’ | sed ‘s/^[ \t]*//g’ | awk –F‘[T ]’ ‘{ print $1; }’` domain_registrar=`cat ${tmp_dir}/whois_info_${project_name}.txt | grep “Registrar:” | awk –F‘: ‘ ‘{ print $2; }’` fi # 第4次检查 if [ “$domain_expir_date” == “” ];then echo “仍没有查询到${domain},换个whios服务器,再重查一遍” timeout $max_time whois –h whois.ename.com –f –d $domain > ${tmp_dir}/whois_info_${project_name}.txt domain_expir_date=`cat ${tmp_dir}/whois_info_${project_name}.txt | grep “Expir” | head –n 1 | awk –F: ‘{ print $2; }’ | sed ‘s/^[ \t]*//g’ | awk –F‘[T ]’ ‘{ print $1; }’` domain_registrar=`cat ${tmp_dir}/whois_info_${project_name}.txt | grep “Registrar:” | awk –F‘: ‘ ‘{ print $2; }’` fi # 检查完,处理结果 if [ “$domain_expir_date” == “” ];then echo “${domain}查询4次失败,放弃该域名查询” echo “$domain” >> ${tmp_dir}/domain_list_result_failure_${project_name}.txt else domain_expir_unixtime=$(date –d “${domain_expir_date}” +%s) the_expir_days=`printf “%1.f\n” $(((${domain_expir_unixtime}–${unixtime_now})/86400))` printf “%-12s%-10s %-8s %-16s %-6s\n” “$domain_expir_unixtime” “$domain_expir_date” “$the_expir_days” “$domain” “$domain_registrar” >> ${tmp_dir}/domain_list_result_succ_tmp_${project_name}.txt if [ “$the_expir_days” –le “$expir_days” ];then printf “%-12s%-10s %-8s %-16s %-6s\n” “$domain_expir_unixtime” “$domain_expir_date” “$the_expir_days” “$domain” “$domain_registrar” >> ${tmp_dir}/domain_list_result_expire_tmp_${project_name}.txt fi fi # 等6秒吧,太频繁,易被whois服务器封掉 sleep 6 done # 打印本地报告 echo “$(date +”%Y–%m–%d %H:%M:%S“) 检查报告” > ${work_dir}/domain_check_result.txt echo “” >> ${work_dir}/domain_check_result.txt echo “### 所有域名检查结果 ###” >> ${work_dir}/domain_check_result.txt printf “%-10s %-8s %-16s %-6s\n” “Expires” “DaysLeft” “Domain” “Registrar” > ${tmp_dir}/domain_list_result_succ_${project_name}.txt echo “———- ——– —————- ——–“ >> ${tmp_dir}/domain_list_result_succ_${project_name}.txt sort –n ${tmp_dir}/domain_list_result_succ_tmp_${project_name}.txt | cut –c13– >> ${tmp_dir}/domain_list_result_succ_${project_name}.txt cat ${tmp_dir}/domain_list_result_succ_${project_name}.txt >> ${work_dir}/domain_check_result.txt echo “” >> ${work_dir}/domain_check_result.txt echo “### ${expir_days}天内到期的域名有 ###” >> ${work_dir}/domain_check_result.txt check_domain_expire_num=`wc –l ${tmp_dir}/domain_list_result_expire_tmp_${project_name}.txt | awk ‘{ print $1; }’` if [ “$check_domain_expire_num” –eq 0 ];then echo “有0个,即全部都在${expir_days}天以上才到期。” >> ${work_dir}/domain_check_result.txt else printf “%-10s %-8s %-16s %-6s\n” “Expires” “DaysLeft” “Domain” “Registrar” > ${tmp_dir}/domain_list_result_expire_${project_name}.txt echo “———- ——– —————- ——–“ >> ${tmp_dir}/domain_list_result_expire_${project_name}.txt sort –n ${tmp_dir}/domain_list_result_expire_tmp_${project_name}.txt | cut –c13– >> ${tmp_dir}/domain_list_result_expire_${project_name}.txt cat ${tmp_dir}/domain_list_result_expire_${project_name}.txt >> ${work_dir}/domain_check_result.txt fi echo “” >> ${work_dir}/domain_check_result.txt echo “### 未能成功检查的域名有 ###” >> ${work_dir}/domain_check_result.txt check_domain_failure_num=`wc –l ${tmp_dir}/domain_list_result_failure_${project_name}.txt | awk ‘{ print $1; }’` if [ “$check_domain_failure_num” –eq 0 ];then echo “有0个,即全部都检查成功。” >> ${work_dir}/domain_check_result.txt else cat ${tmp_dir}/domain_list_result_failure_${project_name}.txt >> ${work_dir}/domain_check_result.txt fi echo “” >> ${work_dir}/domain_check_result.txt echo “有${check_domain_expire_num}个域名将在${expir_days}天内到期,请注意是否续费。本次总共检查了${#domain_array[*]}个域名” >> ${work_dir}/domain_check_result.txt echo “### 报告结束 ###” >> ${work_dir}/domain_check_result.txt echo “” >> ${work_dir}/domain_check_result.txt # 邮件通知报告 if [ “$mailto” == “mailto_simple” ];then sub=“${check_domain_expire_num} domain will expire, please renew” msg=“Such as title.” mailto_simple elif [ “$mailto” == “mailto_advanced” ];then echo –e “<html><head><meta http–equiv=\“Content–Type\“ content=\“text/html; charset=utf–8\“ /> <title>${projectname}_report</title></head><body>“ > ${tmp_dir}/routine_reports_${project_name}.txt sub=“${check_domain_expire_num} domain will expire, please renew” echo “有${check_domain_expire_num}个域名将在${expir_days}天内到期,请注意是否续费。本次总共检查了${#domain_array[*]}个域名” >> ${tmp_dir}/routine_reports_${project_name}.txt echo “” >> ${tmp_dir}/routine_reports_${project_name}.txt echo –e “<pre>” >> ${tmp_dir}/routine_reports_${project_name}.txt # 邮件汇报所有的 echo “### 所有域名检查结果 ###” >> ${tmp_dir}/routine_reports_${project_name}.txt cat ${tmp_dir}/domain_list_result_succ_${project_name}.txt >> ${tmp_dir}/routine_reports_${project_name}.txt # 邮件汇报失败的 echo “” >> ${tmp_dir}/routine_reports_${project_name}.txt echo “### 未能成功检查的域名有 ###” >> ${tmp_dir}/routine_reports_${project_name}.txt check_domain_failure_num=`wc –l ${tmp_dir}/domain_list_result_failure_${project_name}.txt | awk ‘{ print $1; }’` if [ “$check_domain_failure_num” –eq 0 ];then echo “有0个,即全部都检查成功。” >> ${tmp_dir}/routine_reports_${project_name}.txt else cat ${tmp_dir}/domain_list_result_failure_${project_name}.txt >> ${tmp_dir}/routine_reports_${project_name}.txt fi echo –e “</pre>” >> ${tmp_dir}/routine_reports_${project_name}.txt echo –e “</body></html>” >> ${tmp_dir}/routine_reports_${project_name}.txt msg=“$(cat ${tmp_dir}/routine_reports_${project_name}.txt)” mailto_advanced elif [ “$mailto” == “no” ];then echo “no mailto.” >> ${work_dir}/domain_check_result.txt else echo “error, no mailto.” >> ${work_dir}/domain_check_result.txt fi unix2dos ${work_dir}/domain_check_result.txt cat ${work_dir}/domain_check_result.txt } cd $work_dir if [ –f /usr/bin/whois ] && [ –f /usr/bin/dos2unix ] && [ –f /usr/bin/timeout ]; then true else which whois which dos2unix which timeout which unix2dos echo “ERROR: Could not find whois or dos2unix or timeout, try to install” yum install whois jwhois dos2unix unix2dos coreutils –y echo ” Fixed, Please retry” exit fi if [ “$parameter1” == “” ] || [ “$parameter1” == “help” ]; then help_msg exit elif [ “$parameter1” == “do” ]; then check_domain else help_msg exit fi echo “” stop_time=$(date +%s) echo “本次脚本运行了$((${stop_time}-${start_time}))秒。” |
mutt方式的邮件通知结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
有4个域名将在30天内到期,请续费。本次总共检查了29个域名 ### 所有域名检查结果 ### Expires DaysLeft Domain Registrar ————— ———— ———————— ———— 2018–10–15 4 ********.com Name.com, Inc. 2018–10–15 4 ********.net Name.com, Inc. 2018–10–15 4 ********.com 北京新网数码信息技术有限公司 2018–10–15 4 ********.net Name.com, Inc. 2019–01–06 87 ***.com eName Technology Co., Ltd. 2019–02–17 129 *********.biz Name.com, Inc. …… (太多,忽略下) ### 未能成功检查的域名有 ### 有0个,即全部都检查成功。 本次域名到期检查由服务器xxx.xxx.xxx.xxx完成 |
发表回复