DNSPOD Dynamic IP Updater

基于 Bash-no-ip-updater 进行了修改,改得很挫,不过能用就好。

首先需要申请一个 API:


然后获得 domain_id,注意要同时输入 API 的 ID 和 Token。

curl -X POST https://dnsapi.cn/Domain.List -d 'login_token=API_ID,API_TOKEN&format=json' |python -m json.tool

接着获得你要更新的 record_id 和 line_id:

curl -X POST https://dnsapi.cn/Record.List -d 'login_token=API_ID,API_TOKEN&format=json&domain_id=xxx' |python -m json.tool

根据 json 返回的信息,更新命令即可:

#!/bin/bash

# Copyright (C) 2017 Flymemory
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This bash code is based on bash-no-ip-updater: https://github.com/mdmower/bash-no-ip-updater

# Define Parameters

USERAGENT="DNSPOD Dynamic IP Updater"
LOGDIR="/home/flymemory/bash-no-ip-updater"
API_ID=""
API_TOKEN=""
DOMAIN_ID=""
RECORD_ID=""
SUB_DOMAIN="@"
RECORD_TYPE="A"
RECORD_LINE_ID=""

PASSWORD=$(echo -ne $PASSWORD | od -A n -t x1 | tr -d '\n' | sed 's/ /%/g')

if [ ! -d $LOGDIR ]; then
    mkdir -p $LOGDIR
    if [ $? -ne 0 ]; then
        echo "Log directory could not be created or accessed."
        exit 1
    fi
fi

LOGFILE=${LOGDIR%/}/noip.log
IPFILE=${LOGDIR%/}/last_ip

if [ ! -e $LOGFILE ] || [ ! -e $IPFILE ]; then
    touch $LOGFILE $IPFILE
    if [ $? -ne 0 ]; then
        echo "Log files could not be created. Is the log directory writable?"
        exit 1
    fi
elif [ ! -w $LOGFILE ] || [ ! -w $IPFILE ]; then
    echo "Log files not writable."
    exit 1
fi

# Functions

# IP Validator
# http://www.linuxjournal.com/content/validating-ip-address-bash-script
function valid_ip() {
    local  ip=$1
    local  stat=1

    if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        OIFS=$IFS
        IFS='.'
        ip=($ip)
        IFS=$OIFS
        [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
            && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
        stat=$?
    fi
    return $stat
}

# Program

NOW=$(date '+%s')


GET_IP_URLS[0]="http://icanhazip.com"
GET_IP_URLS[1]="http://wtfismyip.com/text"
GET_IP_URLS[2]="http://nst.sourceforge.net/nst/tools/ip.php"
GET_IP_URLS[3]="http://checkip.dyndns.org"

GIP_INDEX=0
while [ -n "${GET_IP_URLS[$GIP_INDEX]}" ] && ! valid_ip $NEWIP; do
    NEWIP=$(curl -s ${GET_IP_URLS[$GIP_INDEX]} | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
    let GIP_INDEX++
done

if ! valid_ip $NEWIP; then
    LOGDATE="[$(date +'%Y-%m-%d %H:%M:%S')]"
    LOGLINE="Could not find current IP"
    echo $LOGLINE
    echo "$LOGDATE $LOGLINE" >> $LOGFILE
    exit 1
fi

LAST_IP=$(cat $IPFILE)

if [ $LAST_IP != $NEWIP ]; then

	UPDATE_IP="login_token=$API_ID,$API_TOKEN&format=json&domain_id=$DOMAIN_ID&record_id=$RECORD_ID&sub_domain=$SUB_DOMAIN&value=$NEWIP&record_type=$RECORD_TYPE&record_line_id=$RECORD_LINE_ID"

	echo $UPDATE_IP

	curl -X POST https://dnsapi.cn/Record.Modify -d $UPDATE_IP >> $LOGFILE

	LOGDATE="[$(date +'%Y-%m-%d %H:%M:%S')]"

	echo "IP: $NEWIP"
	echo $NEWIP > $IPFILE
	echo $LOGLINE
	echo "$LOGDATE $LOGLINE" >> $LOGFILE

else

	echo "IP unchanged"
	echo "$LOGDATE $LOGLINE IP UNCHANGED: $LAST_IP" >> $LOGFILE

fi

exit 0

参考:bash-no-ip-updater

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.