1.15 วิธีการ Remote อุปกรณ์ Mikrotik โดยใช้ DDNS

วิธีการติดตั้ง และปรับแต่งอุปกรณ์ Mikrotik
ให้รองรับการทำงานที่หลากหลายความต้องการ
การติดตั้ง HotSpot Server, การติดตั้ว Load Balanced ฯลฯ

1.15 วิธีการ Remote อุปกรณ์ Mikrotik โดยใช้ DDNS

โพสต์โดย jadeson เมื่อ จันทร์ 25 มิ.ย. 2012 12:40 am

รูปภาพ 1.15 วิธีการ Remote อุปกรณ์ Mikrotik โดยใช้ DDNS
บทความสำหรับ: ผู้ที่มีความรู้เกี่ยวกับระบบเครือข่ายในระดับเบื้องต้นถึงปานกลาง (v1.1)
โดย วิทวัส โฉมประเสริฐ, ศุภสิทธิ์ ศิริพานิชกร, เจตน์สันติ์ ยาวิลาศ


คำถาม

ปัจจุบันใช้ Internet แบบ ADSL ธรรมดา ซึ่ง IP จริงที่ได้มามีการเปลี่ยนแปลงอยู่ตลอดเวลา ทำให้เวลา Remote เข้ามาเซตค่าอุปกรณ์ มีความยุ่งยาก
ถ้าต้องการทำ DDNS เพื่อให้ง่ายต่อการ Remote เข้ามาเซตค่าอุปกรณ์ ต้องทำอย่างไร ?

คำตอบ

- สมัครและกำหนด hostname ขึ้นมา (ทดสอบใช้ของค่าย DYNDNS.ORG)
- เขียน script และ schedulers ลงบนอุปกรณ์ Mikrotik (แนะนำกับ RouterOS 4.10 ขึ้นไป)

ทดสอบแล้วบนอุปกรณ์ Mikrotik RB750GL, RB450, RB751U-2HnD, 750up ครับ

รูปภาพ



ขั้นตอน - เซตโมเด็ม ADSL ให้เป็นบริดจ์


  1. เข้าไปคอนฟิกโมเด็ม ผ่าน IP บนหน้าเวป Browser ( ตัวทดสอบเป็นโมเด็มที่แถมมาของ 3BB)
    แต่เมนูหลักๆ รุ่นหรือยี่ห้ออื่นๆก็จะคล้ายๆกัน โดยเลือก
    Bridge Mode - 1483 Bridged IP LLC

    รูปภาพ



    ขั้นตอน - เข้าผ่าน WINBOX


    *ให้ทำการเชื่อมต่อ internet ให้ได้ก่อน โดยศึกษาได้จากกระทู้อ้างอิง
    1.1 วิธีทำ Hotspot บน MikroTik RB750G (1 Internet)

  2. เข้าโปรแกรม Winbox แล้วคลิกเลือก mac address MikroTik ที่ broadcast เจอ แล้วกด Connect

    รูปภาพ



    ขั้นตอน - วิธีการตั้งค่า DDNS ให้ Mikrotik


  3. เข้าเมนู SYSTEM > Scripts
    แล้วทำการคลิกที่เครื่องหมาย " + " เพื่อเพิ่ม script

    รูปภาพ

  4. ตั้งชื่อ script เป็น ddns-update ส่วนใน source ให้แก้ไข
    username "usernamedyndns"
    password "passworddyndns"
    hostname "hostname.dyndns.org"

    และทำการคัดลอกโค้ดคำสั่งต่อไปนี้ ลงใน source

    โค้ด: เลือกทั้งหมด

    # Set needed variables
    :local username "usernameddns"
    :local password "passwordddns"
    :local hostname "hostname.dyndns.org"

    :global dyndnsForce
    :global previousIP

    # print some debug info
    :log info ("dyndns-update: username = $username")
    :log info ("dyndns-update: password = $password")
    :log info ("dyndns-update: hostname = $hostname")
    :log info ("dyndns-update: previousIP = $previousIP")

    # get the current IP address from the internet (in case of double-nat)
    /tool fetch mode=http address="checkip.dyndns.org" src-path="/" dst-path="/dyndns.checkip.html"
    :local result [/file get dyndns.checkip.html contents]

    # parse the current IP result
    :local resultLen [:len $result]
    :local startLoc [:find $result ": " -1]
    :set startLoc ($startLoc + 2)
    :local endLoc [:find $result "</body>" -1]
    :local currentIP [:pick $result $startLoc $endLoc]
    :log info "dyndns-update: currentIP = $currentIP"

    # Determine if dyndns update is needed
    # more dyndns updater request details available at http://www.dyndns.com/developers/specs/syntax.html
    :if (($currentIP != $previousIP) || ($dyndnsForce = true)) do={
        :set dyndnsForce false
        :set previousIP $currentIP
        /tool fetch user=$username password=$password mode=http address="members.dyndns.org" src-path="/nic/update?hostname=$hostname&myip=$currentIP" dst-path="/dyndns.txt"
        :local result [/file get dyndns.txt contents]
        :log info ("dyndns-update: Dyndns update needed")
        :log info ("dyndns-update: Dyndns Update Result: ".$result)
        :put ("Dyndns Update Result: ".$result)
    } else={
        :log info ("dyndns-update: No dyndns update needed")
    }



    รูปภาพ

  5. เมื่อสร้าง script เสร็จแล้ว ให้ทดสอบทำการ Run script
    สังเกตุ Log จะมีข้อความแสดงสถานะการ update ip ตามที่ได้มีเขียนไว้ใน script

    รูปภาพ

  6. เช็คสถานะ IP ปัจจุบันที่ได้รับ ว่าตรงกับทาง DYNDNS.ORG

    รูปภาพ

  7. เมนู System > Scheduler
    แล้วคลิกเครื่องหมาย " + " เพื่อเพิ่ม Scheduler สั่งให้สคิปทำงาน

    รูปภาพ

  8. ตั้งชื่อ Schedule เป็น ddns-update โดยกำหนดค่าต่างๆดังนี้
    Start Date : Jan/01/1970 (ตั้งย้อนไว้เผื่อ Mikrotik ถ้ามีการถอดแหล่งจ่ายไฟ จะรีเซตเวลาใหม่หมด)
    Start Time: 00:00:00
    Interval: 00:05:00 ( กำหนดให้ทำงานทุกๆ 5 นาที) ผู้ใช้สามารถปรับเป็น "0d ชั่วโมง:นาที:วินาที " ตามที่ต้องการได้
    On Event: ddns-update

    รูปภาพ

  9. หลังจากทำการแก้ไขต่างๆเสร็จเรียบร้อยแล้ว
    ทดสอบทำการ dis-conect และ concect ใหม่เพื่อให้ IP มีการเปลี่ยนแปลง
    สังเกต Log จะมี Status แสดงสถานะการ dis-connect และ connect

    รูปภาพ

  10. รอเวลา ตามที่เรากำหนด script ก็จะทำงาน
    สังเกตุ Run Count ที่ scheduler ว่าจะนับเพิ่มไปเรื่อยๆตามจำนวนครัั้งที่มีการทำงาน
    ส่วน Log ก็จะแสดงให้เห็นว่า IP เดิม คืออะไร มีการ Update ไหม ถ้ามีก็จะอัพเดรตเป็น IP อะไรขึ้นมาให้เห็นเลย

    รูปภาพ

  11. เปรียบเทียบ IP ปัจจุบันที่ได้ ว่าตรงกับทาง DYNDNS.ORG

    รูปภาพ



    ทดสอบ Remote จากภายนอกโดยผ่านทั้ง Browser และ Winbox

  12. ทดสอบเข้าผ่าน Browser บนมือถือ Android
    สังเกตุ browser รูปที่ 4 แสดงสถานะการ Login ผ่าน Web

    รูปภาพ

    รูปภาพ

    รูปภาพ

    รูปภาพ

  13. ทดสอบเข้าผ่าน Winbox
    สังเกตุ winbox รูปที่ 3 แสดงสถานะการ Login ผ่าน winbox

    รูปภาพ

    รูปภาพ

    รูปภาพ



ข้อมูลสินค้าแบบมัลติมีเดีย (Multimedia)







จบบทความ

รายละเอียดการให้บริการจากทีมงาน SYS2U.COM Xpert Zone
ภาพประจำตัวสมาชิก
jadeson
 
โพสต์: 619
ลงทะเบียนเมื่อ: อังคาร 03 ก.พ. 2009 4:50 pm

Re: 1.15 วิธีการ Remote อุปกรณ์ Mikrotik โดยใช้ DDNS

โพสต์โดย jadeson เมื่อ ศุกร์ 04 ต.ค. 2013 4:29 pm

เพิ่มเติม สำหรับผู้ใช้ต้องการกำหนด ddns ให้ WAN2
(สามารถดัดแปลงไปใส่ WAN3, WAN4 ได้เลย)

**ส่วนที่ต้องแก้ไขในโค้ด
1. "DDNSUSER" ให้ใส่ user ของ ddns ที่เราสมัครไว้ (ตัวอย่างใช้ ของ dyndns.org)
2. "DDNSPASSWORD" ใส่ password ของ user
3. "pppoe-out2" ชื่อต้องตรงกับขา WAN2 ที่ได้ตั้งไว้
4. "basisware2.dyndns.info" เป็นชื่อ domain ที่กำหนดจะให้ส่ง IP ไป

โค้ด: เลือกทั้งหมด
:local ddnsuser "DDNSUSER"
:local ddnspass "DDNSPASSWORD"
:local theinterface "pppoe-out2"
:local ddnshost "basisware2.dyndns.info"
:local ipddns [:resolve $ddnshost];
:local ipfresh [ /ip address get [/ip address find interface=$theinterface ] address ]
:if ([ :typeof $ipfresh ] = nil ) do={
:log info ("dyndns-update-wan2: No ip address on $theinterface .")
} else={
:for i from=( [:len $ipfresh] -- 1) to=0 do={
:if ( [:pick $ipfresh $i] = "/") do={
:set ipfresh [:pick $ipfresh 0 $i];
}
}

:if ($ipddns != $ipfresh) do={
:log info ("dyndns-update-wan2: IP-DynDNS = $ipddns")
:log info ("dyndns-update-wan2: IP-Fresh = $ipfresh")
:log info "dyndns-update-wan2: Update IP needed, Sending UPDATE...!"
:local str "/nic/update?hostname=$ddnshost&myip=$ipfresh&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
/tool fetch address=members.dyndns.org src-path=$str mode=http user=$ddnsuser password=$ddnspass dst-path=("/DynDNS.".$ddnshost)
:delay 1
:local str [/file find name="DynDNS.$ddnshost"];
/file remove $str
:global ipddns $ipfresh
:log info "dyndns-update-wan2: Dyndns Update Result $ipfresh!"
} else={
:log info "dyndns-update-wan2: No dyndns update needed";
}
}


code-ddns-wan2.jpg
code-ddns-wan2.jpg (141.79 KiB) เปิดดู 56963 ครั้ง
SYS2U.COM 24-Hour Online IT Store
ซิสทูยู เฟสบุ๊ค - http://www.facebook.com/SYS2UOnline

ภาพประจำตัวสมาชิก
jadeson
 
โพสต์: 619
ลงทะเบียนเมื่อ: อังคาร 03 ก.พ. 2009 4:50 pm

Re: 1.15 วิธีการ Remote อุปกรณ์ Mikrotik โดยใช้ DDNS

โพสต์โดย jadeson เมื่อ เสาร์ 04 ก.ค. 2015 8:02 pm

สำหรับผู้ที่ใช้งาน Mikrotik เวอร์ชั่น 6.xx

สามารถใช้ code นี้แทนได้ครับ

โค้ด: เลือกทั้งหมด
:global ddnsuser "theddnsusername"
:global ddnspass "theddnspassword"
:global theinterface "interfacename"
:global ddnshost blabla.dyndns.org
:global ipddns [:resolve $ddnshost];
:global ipfresh [ /ip address get [/ip address find interface=$theinterface ] address ]
:if ([ :typeof $ipfresh ] = nil ) do={
   :log info ("DynDNS: No ip address on $theinterface .")
} else={
   :for i from=( [:len $ipfresh] - 1) to=0 do={
      :if ( [:pick $ipfresh $i] = "/") do={
    :set ipfresh [:pick $ipfresh 0 $i];
      }
}

:if ($ipddns != $ipfresh) do={
    :log info ("DynDNS: IP-DynDNS = $ipddns")
    :log info ("DynDNS: IP-Fresh = $ipfresh")
   :log info "DynDNS: Update IP needed, Sending UPDATE...!"
   :global str "/nic/update?hostname=$ddnshost&myip=$ipfresh&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
   /tool fetch address=members.dyndns.org src-path=$str mode=http user=$ddnsuser
         password=$ddnspass dst-path=("/DynDNS.".$ddnshost)
    :delay 1
    :global str [/file find name="DynDNS.$ddnshost"];
    /file remove $str
    :global ipddns $ipfresh
  :log info "DynDNS: IP updated to $ipfresh!"
    } else={
     :log info "DynDNS: dont need changes";
    }
}


เพิ่มเติมส่วน Scheduler:

โค้ด: เลือกทั้งหมด
/system scheduler
add interval=1m name=DynDns on-event=DynDns policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api start-time=startup


credit: wiki.mikrotik.com
SYS2U.COM 24-Hour Online IT Store
ซิสทูยู เฟสบุ๊ค - http://www.facebook.com/SYS2UOnline

ภาพประจำตัวสมาชิก
jadeson
 
โพสต์: 619
ลงทะเบียนเมื่อ: อังคาร 03 ก.พ. 2009 4:50 pm

Re: 1.15 วิธีการ Remote อุปกรณ์ Mikrotik โดยใช้ DDNS

โพสต์โดย tanarat2012 เมื่อ จันทร์ 05 ธ.ค. 2016 10:02 pm

ถามนิดนึงครับ ผมใช้ internet ais fiber ครับ ซึ่ง ais ได้ทำ NAT มาครับ ผมจำเป็นต้องใช้ ddns ของทาง ais ครับ
พอจะมีรูปแบบสคริปของ ais ไหมครับ

https://www.thddns.net **นี่เป็น web ddns ของ ais ครับ
tanarat2012
 
โพสต์: 25
ลงทะเบียนเมื่อ: จันทร์ 17 ธ.ค. 2012 2:32 pm

Re: 1.15 วิธีการ Remote อุปกรณ์ Mikrotik โดยใช้ DDNS

โพสต์โดย jadeson เมื่อ พุธ 11 ม.ค. 2017 1:19 pm

tanarat2012 เขียน:ถามนิดนึงครับ ผมใช้ internet ais fiber ครับ ซึ่ง ais ได้ทำ NAT มาครับ ผมจำเป็นต้องใช้ ddns ของทาง ais ครับ
พอจะมีรูปแบบสคริปของ ais ไหมครับ

https://www.thddns.net **นี่เป็น web ddns ของ ais ครับ


- AIS Fibre ที่ออฟฟิศไม่ได้ใช้น่ะครับ แต่เท่าที่ลองหาข้อมูลดู เหมือน ais fibre เค้าไม่ได้แจก IP จริงมาให้น่ะครับ

* DDNS บน Mikrotik จะใช้งานได้ ขา WAN ที่ออกเน็ต จะต้องเป็น IP จริง (Public IP) ครับ

ขอบคุณครับ
SYS2U.COM 24-Hour Online IT Store
ซิสทูยู เฟสบุ๊ค - http://www.facebook.com/SYS2UOnline

ภาพประจำตัวสมาชิก
jadeson
 
โพสต์: 619
ลงทะเบียนเมื่อ: อังคาร 03 ก.พ. 2009 4:50 pm


ย้อนกลับไปยัง วิธีการติดตั้งและปรับแต่งอุปกรณ์ Mikrotik

ผู้ใช้งานขณะนี้

กำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และ บุคคลทั่วไป 7 ท่าน