Last active
April 30, 2017 08:00
-
-
Save ysc3839/5ebf6483b10489964cd0ea2477544ecd to your computer and use it in GitHub Desktop.
cloudxns.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
[ "$1" = "" ] && exit 1 | |
API_KEY="" | |
SECRET_KEY="" | |
DOMAIN="" | |
HOST="" | |
IP="$1" | |
LAST_IP="" | |
LAST_IP_FILE="/tmp/.LAST_IP" | |
if [ -f "$LAST_IP_FILE" ] ; then | |
. "$LAST_IP_FILE" | |
fi | |
logger -t CloudXNS "目前 IP: ${IP} 上次 IP: ${LAST_IP}" | |
if [ "$IP" = "$LAST_IP" ] ; then | |
logger -t CloudXNS "Already up-to-date." | |
exit 0 | |
fi | |
URL_D="https://www.cloudxns.net/api2/domain" | |
DATE=$(date) | |
HMAC_D=$(printf "%s" "$API_KEY$URL_D$DATE$SECRET_KEY"|md5sum|cut -d" " -f1) | |
DOMAIN_ID=$(curl -s $URL_D -H "API-KEY: $API_KEY" -H "API-REQUEST-DATE: $DATE" -H "API-HMAC: $HMAC_D"|grep -o "id\":\"[0-9]*\",\"domain\":\"$DOMAIN"|grep -o "[0-9]*"|head -n1) | |
logger -t CloudXNS "DOMAIN ID: $DOMAIN_ID" | |
URL_R="https://www.cloudxns.net/api2/record/$DOMAIN_ID?host_id=0&row_num=500" | |
HMAC_R=$(printf "%s" "$API_KEY$URL_R$DATE$SECRET_KEY"|md5sum|cut -d" " -f1) | |
RECORD_ID=$(curl -s "$URL_R" -H "API-KEY: $API_KEY" -H "API-REQUEST-DATE: $DATE" -H "API-HMAC: $HMAC_R"|grep -o "record_id\":\"[0-9]*\",\"host_id\":\"[0-9]*\",\"host\":\"$HOST\""|grep -o "record_id\":\"[0-9]*"|grep -o "[0-9]*") | |
logger -t CloudXNS "RECORD ID: $RECORD_ID" | |
URL_U="https://www.cloudxns.net/api2/record/$RECORD_ID" | |
PARAM_BODY="{\"domain_id\":\"$DOMAIN_ID\",\"host\":\"$HOST\",\"value\":\"$IP\"}" | |
HMAC_U=$(printf "%s" "$API_KEY$URL_U$PARAM_BODY$DATE$SECRET_KEY"|md5sum|cut -d" " -f1) | |
RESULT=$(curl -s "$URL_U" -X PUT -d "$PARAM_BODY" -H "API-KEY: $API_KEY" -H "API-REQUEST-DATE: $DATE" -H "API-HMAC: $HMAC_U" -H 'Content-Type: application/json') | |
logger -t CloudXNS "$RESULT" | |
if [ "$(printf "%s" "$RESULT"|grep -c -o "message\":\"success\"")" = 1 ] ; then | |
logger -t CloudXNS "Update success!" | |
echo "LAST_IP=\"$IP\"" > "$LAST_IP_FILE" | |
else | |
logger -t CloudXNS "Update failed!" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
### Custom user script | |
### Called after internal WAN up/down action | |
### $1 - WAN action (up/down) | |
### $2 - WAN interface name (e.g. eth3 or ppp0) | |
### $3 - WAN IPv4 address | |
if [ $1 == "up" ] ; then | |
/etc/storage/scripts/cloudxns.sh $3 & | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment