Skip to content

Instantly share code, notes, and snippets.

@autosquid
Created April 14, 2018 14:59
Show Gist options
  • Save autosquid/54d08fd875ec77cbfd0bd09ac6010930 to your computer and use it in GitHub Desktop.
Save autosquid/54d08fd875ec77cbfd0bd09ac6010930 to your computer and use it in GitHub Desktop.
if [ "$#" != 1 ];then
echo "param error."
exit 0
fi
ACCOUNT=""
PASSWORD=""
DOMAIN=""
SUBDOMAIN=""
RECORD_LINE=""
DOMAIN_ID=""
RECORD_LIST=""
i=0;
dnspod_load_config(){
cfg=$1;
content=`cat ${cfg}`;
ACCOUNT=`echo "${content}" |grep 'ACCOUNT'| sed 's/^ACCOUNT=[\"]\(.*\)[\"]/\1/'`;
PASSWORD=`echo "${content}" |grep 'PASSWORD'| sed 's/^PASSWORD=[\"]\(.*\)[\"]/\1/'`;
DOMAIN=`echo "${content}" |grep 'DOMAIN='| sed 's/^DOMAIN=[\"]\(.*\)[\"]/\1/'`;
RECORD_LINE=`echo "${content}" |grep 'RECORD_LINE'| sed 's/^RECORD_LINE=[\"]\(.*\)[\"]/\1/'`;
SUBDOMAIN=${DOMAIN%%.*}
DOMAIN=${DOMAIN#*.}
}
dnspod_is_record_updated(){
resolve_ip=$(curl -s http://119.29.29.29/d?dn=mi.tanst.net)
current_ip=$(curl -s icanhazip.com)
echo $resolve_ip
echo $current_ip
if [ "$resolve_ip" = "$current_ip" ]; then
echo "Record updated."
exit 0;
fi
}
dnspod_domain_get_id(){
options="login_email=${ACCOUNT}&login_password=${PASSWORD}";
out=$(curl -s -k https://dnsapi.cn/Domain.List -d ${options});
for line in $out;do
if [ $(echo $line|grep '<id>' |wc -l) != 0 ];then
DOMAIN_ID=${line%<*};
DOMAIN_ID=${DOMAIN_ID#*>};
#echo "domain id: $DOMAIN_ID";
fi
if [ $(echo $line|grep '<name>' |wc -l) != 0 ];then
DOMAIN_NAME=${line%<*};
DOMAIN_NAME=${DOMAIN_NAME#*>};
#echo "domain name: $DOMAIN_NAME";
if [ "$DOMAIN_NAME" = "$DOMAIN" ];then
break;
fi
fi
done
out=$(curl -s -k https://dnsapi.cn/Record.List -d "${options}&domain_id=${DOMAIN_ID}")
for line in $out;do
if [ $(echo $line|grep '<id>' |wc -l) != 0 ];then
RECORD_ID=${line%<*};
RECORD_ID=${RECORD_ID#*>};
#echo "record id: $RECORD_ID";
fi
if [ $(echo $line|grep '<name>' |wc -l) != 0 ];then
RECORD_NAME=${line%<*};
RECORD_NAME=${RECORD_NAME#*>};
#echo "record name: $RECORD_NAME";
if [ "$RECORD_NAME" = "$SUBDOMAIN" ];then
break;
fi
fi
done
echo "$RECORD_NAME:$RECORD_ID"
}
dnspod_update_record_ip(){
curl -k https://dnsapi.cn/Record.Ddns -d "login_email=${ACCOUNT}&login_password=${PASSWORD}&domain_id=${DOMAIN_ID}&record_id=${RECORD_ID}&sub_domain=${RECORD_NAME}&record_line=${RECORD_LINE}"
curl -k https://www.xdty.org/mail.php -X POST -d "event=ip($current_ip) changed&name=$SUBDOMAIN&email=$ACCOUNT"
}
main(){
dnspod_load_config $1
dnspod_is_record_updated
dnspod_domain_get_id
dnspod_update_record_ip
}
main $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment