#!/bin/bash # # check env command rpm -q jq bind-utils | \ grep -oP '(?<=package )\S+' | \ xargs -r yum install -y # get public address current_ip=`curl -s ip.sb` &> /dev/null # get your own token and domain info echo "Please input your dnspod-api token_id:" read api_id echo "Please input your dnspod-api token:" read api_token echo "Please input the 'main_domain_name':" read domain_direct echo "Please input the 'sub_domain_prefix':" read domain_pre dns_ip=`host ${domain_pre}.$domain_direct | grep -oP '(?<=address )\N+'` [ -z "$dns_ip" ] && \ echo "set a random dns record to sub_domain:${domain_pre}.$domain_direct first." && exit 11 # get domain_id domain_id=`curl -s 'https://dnsapi.cn/Domain.List' \ -d "login_token=$api_id,$api_token&format=json" | \ jq -r ".domains[]|select(.name==\"$domain_direct\")|.id"` [ -z "$domain_id" ] && exit 22 # get record_id record_id=`curl -s 'https://dnsapi.cn/Record.List' \ -d "login_token=${api_id},${api_token}&format=json&domain_id=$domain_id" | \ jq -r ".records[]|select(.name==\"$domain_pre\")|.id"` [ -z "$record_id" ] && exit 22 # judge ip and update dns if [ -n "$current_ip" ] then if [ "$current_ip" != "$dns_ip" ] then curl -sk \ -X POST 'https://dnsapi.cn/Record.Ddns' \ -d "login_token=$api_id,$api_token&format=json&domain_id=$domain_id&record_id=$record_id&sub_domain=$domain_pre&record_line=默认" >> /sanshi/ddns.log [ $? -ne 0 ] && \ echo -e "\n$(date '+%F-%T')=>\033[31m 动态域名解析失败,请检查脚本!\033[0m" >> /sanshi/ddns.log && \ exit 1 echo -e "\n$(date '+%F-%T')=>\033[35m 公网ip发生变化,并获取成功!\033[0m" >> /sanshi/ddns.log else echo -e "$(date '+%F-%T')=>\033[35m 公网ip没改动!\033[0m" >> /sanshi/ddns.log exit 0 fi else curl -s cip.cc | \ awk -F'[: ]+' '$1~/IP/{print $2}' > /tmp/ip_temp 2> /dev/null current_ip=`cat /tmp/ip_temp` if [ "$current_ip" != "$dns_ip" ]; then curl -sk \ -X POST 'https://dnsapi.cn/Record.Ddns' \ -d "login_token=$api_id,$api_token&format=json&domain_id=$domain_id&record_id=$record_id&sub_domain=$domain_pre&record_line=默认" >> /sanshi/ddns.log [ $? -ne 0 ] && \ echo -e "\n$(date '+%F-%T')=>\033[31m 动态域名解析失败,请检查脚本!\033[0m" >> /sanshi/ddns.log && \ exit 1 echo -e "\n$(date '+%F-%T')=>\033[35m 公网ip发生变化,并获取成功!\033[0m" >> /sanshi/ddns.log fi fi exit 0