Monday 12 August 2013

MikroTIk ใช้งานกับ Dynamic DNS หรือ NoIP

วันนี้เราจะมาพูดถึงเรื่องการทำ DDNS กับ MikroTik นะครับ

ก่อนอื่นต้องพูดถึง DNS คืออะไรก่อนครับ DNS ก็คือการที่เราจะสามารถเข้าถึงอุปกรณ์ของเราที่ต่ออยู่กบ Internet ได้โดยการเรียกผ่านชื่อครับ ไม่จำเป็นต้องจำ IP อีกต่อไปเช่น google.com แต่ทว่าการทำ การที่จะทำ DNS ได้นั่นเราจำเป็นที่จะต้องมี Static IP ครับหรือว่า Lead Line Internet นั่นเองครับ แต่การจะมี Lead Line ได้นั่นต้องจ่ายค่าเช่าโหดครับประมาณเดือนละ 3000 - 4000 บาทสำหรับความเร็วต่ำๆ ครับ

การที่ต้องลงทุนอย่างนั่นมันค่อนข้างจะหนักเอาการอยู่ครับ แล้วอินเตอร์เน็ตที่เราใช้ตามบ้านละใช้ได้มั้ย ใช้ไม่ได้ครับเพราะ IP จะเปลี่ยนทุกครั้งที่ Reboot Modem หรือว่าทุกๆ 24 ชม ครับ

ดังนั่นการที่จะทำให้เน็ตบ้านสามารถทำตัวเหมือน Lead Line ได้นั่นเราจำเป็นต้องพึ่งพาบริการจึงมีที่เรียกว่า DDNS (Dynamic DNS) ครับต้ว DDNS จะทำหน้าที่ในการ Mapping ว่าตอนนี้เราใช้ IP อะไรอยู่และจะทำการอัพเดท IP ปัจจุบันไว้เมื่อมีการร้องของ IP จากชื่อ DNS เราก็จะได้ IP ณ ตอนนั่นไปไงครับ ดังนั่นรูปด้านล่างครับ



เรามาเริ่มวิธีทำกันเลยครับ เริ่มแรกเลยเตรียม Host ที่เราจะใช้กับ No - IP ก่อนเลยครับ ถ้าไม่มี Login ทีก็สมัครไปนะครับ


เริ่มเพิ่ม Host ที่เราต้องการครับในที่นี้ผมจะใช้ mikrotikshop.no-ip.org นะครับ





หลังจากสร้าง Host ของเราเสร็จเราก็มาตั้งค่าใน MikroTik กันครับ
เริ่มจากสร้าง Script ก่อนเลยครับ



ก็อบปี้ Source ได้จากที่นี้ครับ

# No-IP automatic Dynamic DNS update

#--------------- Change Values in this section to match your setup ------------------

# No-IP User account info
:local noipuser "USERNAME"
:local noippass "PASSWORD"

# Set the hostname or label of network to be updated.
# Hostnames with spaces are unsupported. Replace the value in the quotations below with your host names.
# To specify multiple hosts, separate them with commas.
:local noiphost "HOST"

# Change to the name of interface that gets the dynamic IP address
:local inetinterface "ether1-gateway"

#------------------------------------------------------------------------------------
# No more changes need

:global previousIP

:if ([/interface get $inetinterface value-name=running]) do={
# Get the current IP on the interface
   :local currentIP [/ip address get [find interface="$inetinterface" disabled=no] address]

# Strip the net mask off the IP address
   :for i from=( [:len $currentIP] - 1) to=0 do={
       :if ( [:pick $currentIP $i] = "/") do={ 
           :set currentIP [:pick $currentIP 0 $i]
       } 
   }

   :if ($currentIP != $previousIP) do={
       :log info "No-IP: Current IP $currentIP is not equal to previous IP, update needed"
       :set previousIP $currentIP

# The update URL. Note the "\3F" is hex for question mark (?). Required since ? is a special character in commands.
       :local url "http://dynupdate.no-ip.com/nic/update\3Fmyip=$currentIP"
       :local noiphostarray
       :set noiphostarray [:toarray $noiphost]
       :foreach host in=$noiphostarray do={
           :log info "No-IP: Sending update for $host"
           /tool fetch url=($url . "&hostname=$host") user=$noipuser password=$noippass mode=http dst-path=("no-ip_ddns_update-" . $host . ".txt")
           :log info "No-IP: Host $host updated on No-IP with IP $currentIP"
       }
   }  else={
       :log info "No-IP: Previous IP $previousIP is equal to current IP, no update needed"
   }
} else={
   :log info "No-IP: $inetinterface is not currently running, so therefore will not update."
}


ต่อจากนั่นมาสร้าง Scheduler ครับให้ทำงานทุกๆ 5 นาทีครับ




เพียงเท่านี้ทุกๆ 5 นาที MikroTik ก็จะส่ง Update ออกไปที่ No - IP Server เพื่อนทำการบอกว่าตอนนี้ IP ที่จริงคืออะไรกันแน่ครับ


3 comments:

  1. แล้วถ้ามี Lead Line ต้อง set อย่างไรครับ ขอบคุณครับ

    ReplyDelete
    Replies
    1. ถ้าเป็ฯ LL ไม่ต้องทำ dyndns ครับสามารถเข้าตรงๆ ผ่าน ip ได้เลยคัรบ

      Delete
  2. แล้วเราใช้ Winbox เข้าทาง Mac Address ของ ไมโครติกไม่ได้เหรอครับ

    ReplyDelete