Skip to content

Instantly share code, notes, and snippets.

@shenjunru
Created January 13, 2020 14:21

Revisions

  1. shenjunru created this gist Jan 13, 2020.
    38 changes: 38 additions & 0 deletions routeros-ddns-dnspod.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    #policy: read,test

    #pppoe-interface
    :local pppoe "pppoe-out1"

    #dnspod-token
    :local token "uid,api-toekn"

    #dnspod-domain
    :local domain "example.com"
    :local subdomain "www"
    :local dname ($subdomain.".".$domain)

    #get-public-ip
    :local ipaddr [/ip address get [/ip address find interface=$pppoe] address]
    :if (0<[:find $ipaddr "/"]) do={
    :set ipaddr [:pick $ipaddr 0 [:find $ipaddr "/"]]
    }

    #get-dns-record
    :local record [/tool fetch url="https://dnsapi.cn/Record.List" http-data="login_token=$token&format=json&domain=$domain&sub_domain=$subdomain&record_type=A" as-value output=user]
    :set record ($record->"data")
    :set record [:pick $record [:find $record "\"records\":"] [:len $record]]
    :local recordid [:pick $record ([:find $record "\"id\":\""]+6) [:find $record "\",\"ttl"]]
    :local recordip [:pick $record ([:find $record "\"value\":\""]+9) [:find $record "\",\"en"]]

    #set-dns-record
    :if ($recordip!=$ipaddr) do={
    :set record [/tool fetch url="https://dnsapi.cn/Record.Ddns" http-data="login_token=$token&format=json&domain=$domain&sub_domain=$subdomain&record_id=$recordid&record_line_id=0&value=$ipaddr" as-value output=user]
    :set record ($record->"data")
    :if (0<[:find $record "\"code\":\"1\""]) do={
    :log info ("DNSPOD [".$dname."] FORM [".$recordip."] TO [".$ipaddr."]")
    } else={
    :log info ("DNSPOD [".$dname."] FAILED: ".$record)
    }
    } else={
    :log info ("DNSPOD [".$dname."] KEEP [".$recordip."]")
    }