:if ([/interface get $WANIF value-name=running]) do={ # Get the current public IP :local currentIP [/ip address get [/ip address find interface=$WANIF ] address]; :set IP4NEW [:pick $currentIP 0 [:find $currentIP"/"]]; # Check if IP has changed :if ($IP4NEW != $IP4CUR) do={ :log info "CF-DDNS: Public IP changed to $IP4NEW, updating" :local cfapi [/tool fetch http-method=put mode=https url=$url check-certificate=no output=user as-value \ http-header-field="Authorization: Bearer $TOKEN,Content-Type: application/json" \ http-data="{"type":"A","name":"$RECORDNAME","content":"$IP4NEW","ttl":120,"proxied":false}"] :set IP4CUR $IP4NEW :log info "CF-DDNS: Host $RECORDNAME updated with IP $IP4CUR" } else={ :log info "CF-DDNS: Previous IP $IP4NEW not changed, quitting" } } else={ :log info "CF-DDNS: $WANIF is not currently running, quitting" }
简单介绍下变量含义,星号内容自行替换:
TOKEN:Cloudflare管理后台中自行创建的API Token,安全起见不要用Global API KEY。
ZONEID:对应Cloudflare域名管理“overview”页面右下能直接看到的 Zone ID值。
:if ([/interface get $WANIF value-name=running]) do={ # Get the current public IP :local currentIP [/ipv6/address/get [:pick [find global interface=$WANIF] 0 ] address ] :set IP6NEW [:pick $currentIP 0 [:find $currentIP"/"]]; # Check if IP has changed :if ($IP6NEW != $IP6CUR) do={ :log info "CF-DDNS: Public IPv6 address changed to $IP6NEW, updating" :local cfapi [/tool fetch http-method=put mode=https url=$url6 check-certificate=no output=user as-value \ http-header-field="Authorization: Bearer $TOKEN,Content-Type: application/json" \ http-data="{\"type\":\"AAAA\",\"name\":\"$RECORDNAME\",\"content\":\"$IP6NEW\",\"ttl\":120,\"proxied\":false}"] :set IP6CUR $IP6NEW :log info "CF-DDNS: Host $RECORDNAME updated with IP $IP6CUR" } else={ :log info "CF-DDNS: Previous IPv6 $IP6NEW not changed, quitting" } } else={ :log info "CF-DDNS: $WANIF is not currently running, quitting" }