侧边栏壁纸
  • 累计撰写 47 篇文章
  • 累计创建 1 个标签
  • 累计收到 25 条评论

目 录CONTENT

文章目录

ros ddns脚本(clouflare)和动态ip回流脚本

Qiui
2025-03-04 / 1 评论 / 0 点赞 / 20 阅读 / 0 字

1. Cloudflare ddns脚本 本脚本是基于视频https://youtu.be/cSTHn2gxScY?si=r0p1PdiWqeddCJ01更改而来. 将原本需要用global api token 改为普通api token 首先需要获取获取records_ID,参考原视频。 Linux/unix终端下,

curl -X GET "https://api.cloudflare.com/client/v4/zones/Zone_ID/dns_records" \
-H "x-auth-email:email" \
-H "x-auth-key:auth-key" \
-H "content-type: application/json"

  新建script,那么填 cf_ddns 如果运行完总是会报红字错误,试试最后加一行空行

# Update Cloudflare DNS IPv4 address script
# RouterOS version >= 7.x is required

# ** CONFIGURE SECTION **

# WAN IPv4 interface
:local wanif "pppoe-out1"

# Cloudflare section
:local email "email"
:local key "auth-key"
:local zoneID "Zone_ID"
:local recordsID "record_ID"

# Domain hostname
:local hostName "ddns域名"

# DNS server for resolution
:local dnsServer "1.1.1.1"

# ** END OF CONFIGURE SECTION **

# Get WAN interface IPv4 address
:local ip4new [/ip address get [/ip address find interface=$wanif] address]
:set ip4new [:pick [:tostr $ip4new] 0 [:find [:tostr $ip4new] "/"]]

:if ([:len $ip4new] = 0) do={
:log error "[Cloudflare DDNS] Could not get IPv4 for interface $wanif"
:error "[Cloudflare DDNS] Could not get IPv4 for interface $wanif"
}

# Use DNS resolve with specified server to get current IP address of the domain
:local resolvedIp [:resolve $hostName server=$dnsServer]

:if ($resolvedIp = "") do={
:log error "[Cloudflare DDNS] Could not resolve $hostName using server $dnsServer"
:error "[Cloudflare DDNS] Could not resolve $hostName using server $dnsServer"
}

:log info "[Cloudflare DDNS] DNS resolved IP for $hostName: $resolvedIp"
:log info "[Cloudflare DDNS] Current WAN IPv4 address: $ip4new"

:if ($ip4new = $resolvedIp) do={
:log info "[Cloudflare DDNS] WAN IPv4 address for interface $wanif and DNS resolved IP are the same, no update needed."
} else {
:log info "[Cloudflare DDNS] WAN IPv4 address for interface $wanif has been changed to $ip4new."

:local url "https://api.cloudflare.com/client/v4/zones/$zoneID/dns_records/$recordsID"
:local header "X-Auth-Email: $email,Authorization: Bearer $key,Content-Type:application/json"
:local data "{\"type\":\"A\",\"name\":\"$hostName\",\"content\":\"$ip4new\",\"ttl\":60,\"proxied\":false}"

:log info "[Cloudflare DDNS] URL: $url"
:log info "[Cloudflare DDNS] HEADER: $header"
:log info "[Cloudflare DDNS] DATA: $data"
:log info "[Cloudflare DDNS] Updating host $hostName address."

:local jsonAnswer [/tool fetch http-method=put mode=https http-header-field=$header http-data=$data url=$url as-value output=user]

:if ([:len $jsonAnswer] > 0) do={

/system script run "JParseFunctions"; local JSONLoads; local JSONUnload
:local result ([$JSONLoads ($jsonAnswer->"data")]->"success")
$JSONUnload

:if ($result = true) do={
:log info "[Cloudflare DDNS] Successfully updated IPv4 address to $ip4new."
} else {
:log error "[Cloudflare DDNS] Error while updating IPv4 address."
}
} else {
:log error "[Cloudflare DDNS] No answer from Cloudflare API."
}
}

  pppoe-out1 使用的profile内的On Up部分加上:(末尾留一行空行

:delay 3s
/system/script/
run cf_ddns

2. 动态ip回流脚本 新建script ,名为“dynamic_nat”

:global addold

:global addnew

:set addnew [/interface get [/interface find name="pppoe-out1"] running]

:if ($addnew=true) do={

:set addold [/ip address get [/ip address find dynamic=yes interface="pppoe-out1"] address]

:set addold [:pick $addold 0 ([:len $addold ] -3)]

/ip firewall nat set [/ip firewall nat find comment="dynamic_nat"] dst-address=$addold

}

  同样的,在pppoe-out1 使用的profile内的On Up部分加上就变成了如下:

#dynamic_nat

:delay 3s
execute "dynamic_nat"

#cf_ddns
/system/script/
run cf_ddns

这样ddns和动态ip脚本就会随着每一次重新pppoe拨号重新运行

0

评论区