อุปกรณ์ Mikrotik ทุกตัวจะมีบริการ Dynamic DNS ติดตั้งมาให้ฟรีพร้อมใช้งานโดยที่กับ Cloud ของ Server Mikrotik เอง แต่ทว่า .. ในช่วง 2-3 ปีที่ผ่านมา Mikrotik Cloud ก็ล่มหลายครั้งจน Network Engineer หลายๆท่าน เริ่มที่จะไม่ใช้บริการตรงนี้แล้ว หันไปใช้ DDNS เจ้าอื่นกันแทน เช่น No-ip หรือ DYNU แต่กลุ่มเหล่านั้นจะมีค่าใช้จ่ายในการใช้งานครับ
ถ้าอยากจะใช้งานฟรี เราสามารถใช้บริการของ Cloudflare มาทำระบบ Dynamic DNS ตรงนี้แทนได้ แต่ว่า มันไม่ได้มี Service บริการตรงนี้เฉพาะ เราจำเป็นที่จะต้องใช้ทั้ง Cloudflare API และ Mikrotik Script ในการทำงานร่วมกัน
ในคลิปนี้ ผมได้สอนวิธีการแบบ Step by Step ในการใช้บริการ Dynamic DNS ของ Cloudflare บนอุปกรณ์ Mikrotik ให้ครับ
ข้อมูลต่างๆในคลิปสามารถเอาได้จากที่นี่ครับ
คำสั่งสำหรับเรียก Cloudflare API ผ่าน CURL เพื่อดึงค่า Record ID
curl -X GET “https://api.cloudflare.com/client/v4/zones/ZONE_ID/dns_records?name=SUB_DOMAIN” \
-H “X-Auth-Email: [email protected]” \
-H “X-Auth-Key: YOUR_API_KEY” \
-H “Content-Type: application/json”
Link สำหรับ Mikrotik Script ของคุณ ChrisG661
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cloudflare Dynamic DNS update script | |
# Required policy: read, write, test, policy | |
# Add this script to scheduler | |
# Install DigiCert root CA or disable check-certificate | |
# Configuration ——————————————————————— | |
:local TOKEN "__APITOKEN__" | |
:local ZONEID "__ZONEIDENTIFIER__" | |
:local RECORDID "__RECORDIDENTIFIER__" | |
:local RECORDNAME "__DNSRECORD__" | |
:local WANIF "__WANINTERFACE__" | |
#———————————————————————————— | |
:global IP4NEW | |
:global IP4CUR | |
:local url "https://api.cloudflare.com/client/v4/zones/$ZONEID/dns_records/$RECORDID/" | |
:if ([/interface get $WANIF value-name=running]) do={ | |
# Get the current public IP | |
:local requestip [tool fetch url="https://ipv4.icanhazip.com" mode=https check-certificate=yes output=user as-value] | |
:set IP4NEW [:pick ($requestip->"data") 0 ([:len ($requestip->"data")]-1)] | |
# 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=yes 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" | |
} |