使用DNSPOD的域名解析实现DDNS域名动态的脚本

摘要

使用shell脚本和dnspod官方提供的api实现域名的ddns动态解析,本脚本只会在ip有改变的时候才对dnspod域名做解析更改,而不是定时去更改。脚本适用Linux和openwrt,前提是有bash

下面是openwrt系统的路由使用方法
1更换openwrt的源加速地址

sed -i 's/downloads.openwrt.org/openwrt.legion.pt/' /etc/opkg.conf

2安装bash shell

opkg update && opkg install bash

3将root用户的shell换成bash

sed -ri 's|^(root:.*:)/bin/.*|\1/bin/bash|' /etc/passwd

4下载DDNS脚本

wget http://www.dwhd.org/script/dnspodDDNS.sh -O /etc/dnspodddns.sh && chmod +x /etc/dnspodddns.sh

5执行下面的命令 修改几个必要的参数

vi /etc/dnspodddns.sh
[ -z "$(grep 'dnspodddns.sh' /var/spool/cron/crontabs/root )" ] && echo "*/1 * * * * /etc/dnspodddns.sh &>/dev/null" >> /var/spool/cron/crontabs/root

6然后退出ssh 重新登录或者重启下路由

reboot

当然也可以使用一键脚本来配置这些繁琐的东西

wget -q http://www.dwhd.org/script/AUTOdnspodDDNS.sh -O /tmp/AUTOdnspodDDNS.sh && bash /tmp/AUTOdnspodDDNS.sh

下面是一键配置脚本的源码

#!/bin/bash
#########################################################################
# File Name: /home/wwwroot/www.05hd.com/AUTOdnspodDDNS.sh
# Author: LookBack
# Email: admon#05hd.com
# Version:
# Created Time: Tue 07 Apr 2015 08:49:50 PM CST
#########################################################################

#================================================================
echo=echo
for cmd in echo /bin/echo; do
        $cmd >/dev/null 2>&1 || continue
        if ! $cmd -e "" | grep -qE '^-e'; then
                echo=$cmd
                break
        fi
done
CSI=$($echo -e "\033[")
CEND="${CSI}0m"
CDGREEN="${CSI}32m"
CRED="${CSI}1;31m"
CGREEN="${CSI}1;32m"
CYELLOW="${CSI}1;33m"
CBLUE="${CSI}1;34m"
CMAGENTA="${CSI}1;35m"
CCYAN="${CSI}1;36m"
CQUESTION="$CMAGENTA"
CWARNING="$CRED"
CMSG="$CCYAN"
#================================================================
clear
[ $(id -u) != "0" ] && echo "警告: 你现在不是root权限登录服务器, 请使用root帐号登录路由器,然后执行本脚本~!" && exit 1

#================================================================
#更改openwrt源地址为亚马逊云加速地址
sed -i 's@downloads.openwrt.org/@openwrt.legion.pt/@' /etc/opkg.conf
opkg update
#给系统添加bash
if [ "$(echo $SHELL)" != "/bin/bash" ]; then
        opkg install bash
        sed -ri 's|^(root:.*:)/bin/.*|\1/bin/bash|' /etc/passwd
fi
#给系统安装VIM
if ! which tar &>/dev/null; then opkg install tar;fi
if which vim &>/dev/null; then opkg remove vim;fi
opkg install vim-full vim-runtime
wget -cq http://www.dwhd.org/script/tar_gz_bz2/vim.tar.gz -O vim.tar.gz
tar xf vim.tar.gz -C /root/ && rm -rf vim.tar.gz && sed -ri 's/(adm)o(n#05hd.com)/\1i\2/' /root/.vimrc

#美化PS1
sed -i '/export PS1/d' /etc/profile
if ! grep 'PS1' /etc/profile &>/dev/null; then
        cat >> /etc/profile << EOF

[ -x /bin/bash ] && PS1='[\[\033[0;34m\]\u\[\033[0;37m\]@\[\033[0;35m\]\h\[\033[0;33m\] \w\[\033[0;37m\]]\[\033[0;31m\]$\[\033[00m\] '
EOF
        . /etc/profile && sed -i '/grep/d' ~/.bashrc && . ~/.bashrc
fi
clear
wget -c -q http://www.dwhd.org/script/dnspodDDNS.sh -O /etc/dnspodddns.sh && chmod +x /etc/dnspodddns.sh

clear
echo "${CQUESTION}请输入你的DNSPOD登录帐号 (提示:一般为电子邮箱):${CEND} " && read DNSPODID
sed -ri "s/(DNSPODID=).*/\1$DNSPODID/" /etc/dnspodddns.sh
echo "${CQUESTION}请输入你的DNSPOD登录密码:${CEND} " && read DNSPODPW
sed -ri "s/(DNSPODPW=).*/\1$DNSPODPW/" /etc/dnspodddns.sh
echo "${CQUESTION}请输入你的DNSPOD需要做DDNS的主域名(例如:abc.com):${CEND} " && read DNSPODPD
sed -ri "s/(DNSPODPD=).*/\1$DNSPODPD/" /etc/dnspodddns.sh
echo "${CQUESTION}请输入你的DNSPOD需要做DDNS的域名名称
(例如:openwrt.legion.pt做DDNS域名就输入openwrt):${CEND}" && read DNSPODSDNM
sed -ri "s/(DNSPODSDNM\[0\]=).*/1${DNSPODSDNM}/" /etc/dnspodddns.sh

[ -z "$(grep 'dnspodddns.sh' /var/spool/cron/crontabs/root )" ] && echo "*/1 * * * * /etc/dnspodddns.sh &>/dev/null" >> /var/spool/cron/crontabs/root
clear && rm -rf $0
bash /etc/dnspodddns.sh && clear
echo "${CYELLOW}  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M

DNSPOD的域名动态更新脚本配置完成,在使用中如有BUG 请按下方联系方式联系

${CWARNING}QQ:393411264 Email: 393411264@qq.com${CEND}"

###########注意######################
如果做了多拨 那么请将脚本的第54行修改成pppoe-wan就是你的wan口名称不是虚拟wan口
WanIP=$(ifconfig pppoe-wan | awk -F'[ ]+|:' '/inet /{if($4!~/^192\.168|^172\.16|^10\.|^127|^0/) print $4}')

使用DNSPOD的域名解析实现DDNS域名动态的脚本


下面是脚本源码

#!/bin/bash
#########################################################################
# File Name: dnspodDDNS.sh
# Author: LookBack
# Email: 393411264@qq.com
# Created Time: 2015年03月28日 星期六 23时19分00秒
#########################################################################
#
#        利用DNSPOD官方提供的API,自动更新域名解析记录,
#        在Netgear 4300 openwrt和CentOS 6系统上测试完美执行,
#        在其他硬件的openwrt上应该也是可以执行的,
#        非openwrt系统的不保证脚本的兼容性。
#        如有任何BUG 请联系我 393411264@qq.com
#
#########################################################################

#=======================下方是需要修改的参数信息=============================================
#DNSPOD 帐号
DNSPODID="393411264@qq.com"
#DNSPOD 密码
DNSPODPW="www.05hd.com"
#DNSPOD 主域名
DNSPODPD="05h.com"
#DNSPOD 二级域名,可以是多个,如下设置DNSPODSDNM[0-n]。有几个就写几个,把多余的删除
DNSPODSDNM[0]="openwrt141"
DNSPODSDNM[1]="openwrt"
DNSPODSDNM[2]="netgear111"
#=========================下方内容如果不懂请勿修改============================================
CURL=$(which curl)
GetID() {
        $CURL -X POST https://dnsapi.cn/Domain.List -ks -d "login_email=$1&login_password=$2&format=json" | \
awk -F',' '{for(i=1;i<=NF;i++) if($i~/"id":[0-9]+/ || $i~/"name":".*"/) {print $i}}' | \
awk '{if (NR%2==0){print $0} else {printf"%s ",$0}}' | sed 's/"domains":\[//' | \
awk -F':| |"' '{print $9,$4}' > /tmp/dnspodDomain
        while read line; do
                if [ "$(awk '{print$1}' <<< $line)A" = "${DNSPODPD}A" ]; then
                        DNSPODPDID=$(awk '{print$2}' <<< $line)
                        break
                fi
        done < /tmp/dnspodDomain

        $CURL -X POST https://dnsapi.cn/Record.List -ks -d "login_email=$1&login_password=$2&format=json&domain_id=$DNSPODPDID" | \
sed 's/.*},"records":\[//' | awk -F',' '{for(i=1;i<=NF;i++) if($i~/"id":"[0-9]+"/ || $i~/"name":".*"/) {print $i}}' | \
awk '{if (NR%2==0){print $0} else {printf"%s ",$0}}' | awk -F'"' '{print $8,$4}' > /tmp/dnspodRecord
        while read line; do
                for i in `seq 1 ${#DNSPODSDNM[@]}`; do
                        i=$[$i-1]
                        [ "$(awk '{print$1}' <<< $line)A" = "${DNSPODSDNM[$i]}A" ] && DNSPODSDID[$i]=$(awk '{print$2}' <<< $line) && break
                done
        done < /tmp/dnspodRecord
}

StartRun() {
        WanIP=$($CURL -s ip.cn | grep -Eo '([0-9]{1,3}[\.]){3}[0-9]{1,3}')
        DNSPODIP=$($CURL -X POST https://dnsapi.cn/Record.Info -ks -d \
"login_email=$1&login_password=$2&format=json&domain_id=$3&record_id=${DNSPODSDID[0]}" | \
grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}')
#awk -F':|,|"' '{for(i=1;i<=NF;i++)if($i~/^([0-9]{1,3}\.){3}[0-9]{1,3}$/){print$i;break}}')
        [ -z $WanIP ] && exit 1
        [ -z $DNSPODIP ] && exit 1
        :> /tmp/dnspodDomain && :>/tmp/dnspodRecord
}

UploadIP() {
        [ "${3}A" != "${4}A" ]
        if [ "A$?" = "A0" ]; then
                for i in `seq 1 ${#DNSPODSDNM[@]}`; do
                        i=$[$i-1]
                        $CURL -X POST https://dnsapi.cn/Record.Modify -ks -d \
"login_email=$1&login_password=$2&format=json&domain_id=$5\
&record_id= ${DNSPODSDID[$i]}&sub_domain=${DNSPODSDNM[$i]}&value=$3\
&record_type=A&record_line=默认" &>/dev/null
                        echo "$(date "+%F %H:%M:%S") Domain ${DNSPODSDNM[$i]}.$DNSPODPD A Record Update To $3" >> /var/log/dnspod.log
                done
        fi
}

RUN() {
        GetID $1 $2
        StartRun $1 $2 $DNSPODPDID
        UploadIP $1 $2 ${WanIP} ${DNSPODIP} $DNSPODPDID ${DNSPODSDID[*]}
}

RUN $DNSPODID $DNSPODPW $DNSPODPDID

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

lookback

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: