侧边栏壁纸
博主头像
惬意小蜗牛博主等级

海内存知己,天涯若比邻!

  • 累计撰写 54 篇文章
  • 累计创建 143 个标签
  • 累计收到 64 条评论

目 录CONTENT

文章目录

腾讯云 DDNS Shell 脚本 - IPV4

惬意小蜗牛
2021-07-24 / 0 评论 / 0 点赞 / 1,706 阅读 / 552 字 / 正在检测是否收录...
#!/bin/bash
echo "[$(date "+%G/%m/%d %H:%M:%S")] DDNSPOD.sh start..."

# 设置需要 DDNSPOD 的 APP_ID 和 API_Token
API_ID=xxxxxx
API_Token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# 设置需要 DDNS 的一级域名 ,格式为 xxx.com
domain=xxx.com

# 设置需要 DDNS 的二级域名 ,格式为 aaa
host=aaa

# 设置获取本机 IP 需要测试的网站地址
CHECKURL="http://ip.03k.org"

. /etc/profile
date
if (echo $CHECKURL | grep -q "://"); then
    IPREX='([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
    URLIP=$(curl -4 -k $(if [ -n "$OUT" ]; then echo "--interface $OUT"; fi) -s $CHECKURL | grep -Eo "$IPREX" | tail -n1)
    if (echo $URLIP | grep -qEvo "$IPREX"); then
        URLIP="Get $DOMAIN URLIP Failed."
    fi
    echo "[URL IP]:$URLIP"
    dnscmd="nslookup"
    type nslookup >/dev/null 2>&1 || dnscmd="ping -c1"
    DNSTEST=$($dnscmd $host.$domain)
    if [ "$?" != 0 ] && [ "$dnscmd" == "nslookup" ] || (echo $DNSTEST | grep -qEvo "$IPREX"); then
        DNSIP="Get $host.$domain DNS Failed."
    else
        DNSIP=$(echo $DNSTEST | grep -Eo "$IPREX" | tail -n1)
    fi
    echo "[DNS IP]:$DNSIP"
    if [ "$DNSIP" == "$URLIP" ]; then
        echo "IP SAME IN DNS,SKIP UPDATE."
        exit
    fi
fi
token="login_token=${API_ID},${API_Token}&format=json&lang=en&error_on_empty=yes&domain=${domain}&sub_domain=${host}"
Record="$(curl -4 -k $(if [ -n "$OUT" ]; then echo "--interface $OUT"; fi) -s -X POST https://dnsapi.cn/Record.List -d "${token}")"
iferr="$(echo ${Record#*code} | cut -d'"' -f3)"
if [ "$iferr" == "1" ]; then
    record_ip=$(echo ${Record#*value} | cut -d'"' -f3)
    echo "[API IP]:$record_ip"
    if [ "$record_ip" == "$URLIP" ]; then
        echo "IP SAME IN API,SKIP UPDATE."
        exit
    fi
    record_id=$(echo ${Record#*\"records\"\:\[\{\"id\"} | cut -d'"' -f2)
    record_line_id=$(echo ${Record#*line_id} | cut -d'"' -f3)
    echo Start DDNS update...
    ddns="$(curl -4 -k $(if [ -n "$OUT" ]; then echo "--interface $OUT"; fi) -s -X POST https://dnsapi.cn/Record.Ddns -d "${token}&record_id=${record_id}&record_line_id=${record_line_id}")"
    ddns_result="$(echo ${ddns#*message\"} | cut -d'"' -f2)"
    echo -n "DDNS upadte result:$ddns_result "
    echo $ddns | grep -Eo "$IPREX" | tail -n1
else
    echo -n Get $host.$domain error :
    echo $(echo ${Record#*message\"}) | cut -d'"' -f2
fi
0

评论区