Skip to content

Instantly share code, notes, and snippets.

@luochen1990
Last active February 10, 2022 09:48
Show Gist options
  • Select an option

  • Save luochen1990/97c62ea2d58bd06d76b22d64ddf275a2 to your computer and use it in GitHub Desktop.

Select an option

Save luochen1990/97c62ea2d58bd06d76b22d64ddf275a2 to your computer and use it in GitHub Desktop.
DDNS (via DNSPOD API)

GET WAN IP Address

> nslookup myip.opendns.com. resolver1.opendns.com

...
Name: myip.opendns.com
Address: 101.69.24.2

OR

> nslookup myip.opendns.com resolver1.opendns.com | grep 'myip.opendns.com' -A 1 | grep 'Address' | awk '{print $2}'
125.120.213.100

101.69.24.2

UPDATE RECORD Via DNSPOD API

DNSPOD API DOC: https://www.dnspod.cn/docs/records.html#dns

Suppose:

  • your domain: mydomain.com
  • your dnspod login token: 94888,d2ea401be61b814fffffffffffffffff
  • your ddns sub domain: myhome (so you can use myhome.mydomain.com to visit)

DNSPOD LOGIN TOKEN (id and token splited by ','): 94888,d2ea401be61b814fffffffffffffffff

GET RECORD LIST

>  curl -X POST https://dnsapi.cn/Record.List -d 'login_token=94888,d2ea401be61b814fffffffffffffffff&format=json&domain=mydomain.com'

{
    "domain": {
        "dnspod_ns": [
            "f1g1ns1.dnspod.net",
            "f1g1ns2.dnspod.net"
        ],
        "ext_status": "",
        "grade": "DP_Free",
        "id": "24788888",
        "min_ttl": 600,
        "name": "mydomain.com",
        "owner": "my-dnspod-account@gmail.com",
        "punycode": "mydomain.com",
        "status": "enable",
        "ttl": 600
    },
    "info": {
        "record_total": "6",
        "records_num": "6",
        "sub_domains": "6"
    },
    "records": [
        ...
        {
            "enabled": "1",
            "id": "420999999",
            "line": "\u9ed8\u8ba4",
            "line_id": "0",
            "monitor_status": "",
            "mx": "0",
            "name": "myhome",
            "remark": "",
            "status": "enabled",
            "ttl": "600",
            "type": "A",
            "updated_on": "2019-04-26 10:31:00",
            "use_aqb": "no",
            "value": "101.69.24.1",
            "weight": null
        }
    ],
    "status": {
        "code": "1",
        "created_at": "2019-04-26 10:46:52",
        "message": "Action completed successful"
    }
}

UPDATE RECORD

> curl -X POST https://dnsapi.cn/Record.Ddns -d 'login_token=94888,d2ea401be61b814fffffffffffffffff&format=json&domain=mydomain.com&record_id=420999999&record_line_id=0&sub_domain=myhome&value=101.69.24.2'


{"status":{"code":"1","message":"Action completed successful","created_at":"2019-04-26 10:54:26"},"record":{"id":420999999,"name":"myhome","value":"101.69.24.2"}}

NOTE: keep value empty will use your remote ip address as default value. In this case you can use nslookup to reduce the API's called time.

@luochen1990
Copy link
Copy Markdown
Author

之前那篇是解释动态域名解析的原理, 这里提供一个最终的DDNS Shell脚本:

首先提供基本信息:

export DNSPOD_API_KEY=94888,d2ea401be61b814fffffffffffffffff      #这里写你的 API_TOKEN, 注意前面要带上ID
export DOMAIN=xxx.com   #这里写你在dnspod上托管的域名
export SUB_DOMAIN=xxx  #这里写子域名, 也支持 xx.yy 等多级子域名

完整脚本:

#!/usr/bin/env sh

#DOC: https://www.dnspod.cn/docs/records.html#dns

export DNSPOD_API_KEY=94888,d2ea401be61b814fffffffffffffffff
export DOMAIN=xxx.com
export SUB_DOMAIN=xxx

# To Debug:
#curl -# -X POST https://dnsapi.cn/Record.List -d "login_token=$DNSPOD_API_KEY&format=json&domain=$DOMAIN&sub_domain=$SUB_DOMAIN&record_type=A" | jq

# To reduce query:
current_ip=$(curl -# cip.cc | head -1 | sed 's/[^0-9]*\([0-9.]*\).*/\1/')
registered_ip=$(curl -# -X POST https://dnsapi.cn/Record.List -d "login_token=$DNSPOD_API_KEY&format=json&domain=$DOMAIN&sub_domain=$SUB_DOMAIN&record_type=A" | jq '.records[0].value' | sed 's/[^0-9]*\([0-9.]*\).*/\1/')
[ "$current_ip" = "$registered_ip" ] && echo 'skip' && exit 0;

RECORD_ID=$(curl -# -X POST https://dnsapi.cn/Record.List -d "login_token=$DNSPOD_API_KEY&format=json&domain=$DOMAIN&sub_domain=$SUB_DOMAIN&record_type=A" | jq '.records[0].id' | tr -d '"')

#printf $RECORD_ID

curl -X POST https://dnsapi.cn/Record.Ddns -d "login_token=$DNSPOD_API_KEY&domain=$DOMAIN&sub_domain=$SUB_DOMAIN&record_id=$RECORD_ID&record_line_id=0&&ttl=600" | jq -c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment