Akses Mikrotik Dengan DynDNS

In Mikrotik

Sebenarnya ini bukan masalah baru, namun karena baru sekarang aku menghadapi masalah ip dinamin di mikrotik jadi aku tulis tutorialnya disini sebagai pengingat saja. Kantor saat ini menggunakan 2 koneksi utama yaitu ISP dan Speedy, karena ISP sering bermasalah pada saat hujan maka webserver yang berada di kantor dapat dipastikan tidak bisa diakses.

Dan kebetulan peraturan baru dari Speedy kalau user yang memilih paket Familia atau 1 mb mulai Oktober atau November kalau tidak salah, tidak akan memperoleh ip statis. Ip statis akan didapat secara otomatis jika memilih paket yang 3mb yang biaya per bulannya kurang lebih 1,6jt.

Nah, untuk menekan biaya akhirnya aku tetap menggunakan ip dinamis Speedy dan menggunakan ip statis ISP yang kalau musim hujan sering mati. Banyak tutorial yang menggunakan fitur changeip.com, no-ip.com, changeip.net tapi aku lebih familiar dan nyaman pakai dyndns.com dan setelah dicoba juga sukses menggunakan dyndns.com ūüėÄ .

Berikut script dyndns untuk mikrotik yang menggunakan ip dinamis :

Buat script dengan nama file dynDNS :

# Define User Variables
:global ddnsuser "DYNDNSUSER"
:global ddnspass "DYNDNSPASS"
:global ddnshost "DYNDNSHOST"

# Define Global Variables
:global ddnsip
:global ddnslastip
:if ([ :typeof $ddnslastip ] = nil ) do={ :global ddnslastip "0" }

:global ddnsinterface
:global ddnssystem ("mt-" . [/system package get system version] )

# Define Local Variables
:local int

# Loop thru interfaces and look for ones containing
# default gateways without routing-marks
:foreach int in=[/ip route find dst-address=0.0.0.0/0 active=yes ] do={
  :if ([:typeof [/ip route get $int routing-mark ]] != str ) do={
     :global ddnsinterface [/ip route get $int interface]
  }
}

# Grab the current IP address on that interface.
:global ddnsip [ /ip address get [/ip address find interface=$ddnsinterface ] address ]

# Did we get an IP address to compare?
:if ([ :typeof $ddnsip ] = nil ) do={
   :log info ("DynDNS: No ip address present on " . $ddnsinterface . ", please check.")
} else={
  :if ($ddnsip != $ddnslastip) do={
    :log info "DynDNS: Sending UPDATE!"
    :local str "/nic/update?hostname=$ddnshost&myip=$ddnsip&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 ddnslastip $ddnsip
  }
}

Buat scheduler di mikrotik untuk menjalankan script dynDNS :

/system scheduler add name=dynDNS interval=00:01 on-event="/system script run dynDns ;"

 

Untuk Mikrotik versi 4.xx :

# Set needed variables
:local username "YourUsername"
:local password "YourPassword"
:local hostname "nohostset"
:global systemname [/system identity get name]

:if ($systemname  = "Site1" ) do= {
:set hostname "yourdomain1.dyndns.org"
}
:if ($systemname  = "Site2" ) do= {
:set hostname "yourdomain2.dyndns.org"
}
:if ($systemname  = "Site3" ) do= {
:set hostname "yourdomain3.dyndns.org"
}

:global dyndnsForce
:global previousIP

# print some debug info
:log info ("UpdateDynDNS: username = $username")
:log info ("UpdateDynDNS: password = $password")
:log info ("UpdateDynDNS: hostname = $hostname")
:log info ("UpdateDynDNS: 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 "UpdateDynDNS: currentIP = $currentIP"

# Remove the # on next line to force an update every single time - useful for debugging,
#  but you could end up getting blacklisted by DynDNS!

#:set dyndnsForce true

# Determine if dyndns update is needed
# more dyndns updater request details 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 ("UpdateDynDNS: Dyndns update needed")
    :log info ("UpdateDynDNS: Dyndns Update Result: ".$result)
    :put ("Dyndns Update Result: ".$result)
} else={
    :log info ("UpdateDynDNS: No dyndns update needed")
}

jika masih mengalami masalah dengan script diatas, silahkan coba script berikut :

:local ddnsuser "your user name"
:local ddnspass "your password"
:local theinterface "device to renove IP"
:local ddnshost "yourhost.dyndns.org"
:local ipddns [:resolve $ddnshost];
:local 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...!"
   :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: IP updated to $ipfresh!"
    } else={
     :log info "DynDNS: dont need changes";
    }
}

 

Untuk Mikrotik versi 5.xx :

# Set needed variables
:local username "YOURUSER"
:local password "YOURPASWORD"
:local hostname "YOURHOSTNAME.dyndns.org"

:global dyndnsForce
:global previousIP 

# print some debug info
:log info ("UpdateDynDNS: username = $username")
:log info ("UpdateDynDNS: password = $password")
:log info ("UpdateDynDNS: hostname = $hostname")
:log info ("UpdateDynDNS: 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"
:delay 1
: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 "UpdateDynDNS: currentIP = $currentIP"

# Remove the # on next line to force an update every single time - useful for debugging,
# but you could end up getting blacklisted by DynDNS!

#:set dyndnsForce true

# Determine if dyndns update is needed
# more dyndns updater request details http://www.dyndns.com/developers/specs/syntax.html

:if (($currentIP != $previousIP) || ($dyndnsForce = true)) do={
   :set dyndnsForce false
   :set previousIP $currentIP
   :log info "$currentIP or $previousIP"
   /tool fetch user=$username password=$password mode=http address="members.dyndns.org" 
      src-path="nic/update?system=dyndns&hostname=$hostname&myip=$currentIP&wildcard=no" 
      dst-path="/dyndns.txt"
   :delay 1
   :local result [/file get dyndns.txt contents]
   :log info ("UpdateDynDNS: Dyndns update needed")
   :log info ("UpdateDynDNS: Dyndns Update Result: ".$result)
   :put ("Dyndns Update Result: ".$result)
} else={
   :log info ("UpdateDynDNS: No dyndns update needed")
}

 

Script dyndns diatas belum benar2 bisa mengakses server yang berada di belakang NAT mikrotik, nah untuk dapat mengakses server yang berada di belakang NAT script diatas perlu dimodifikasi sedikit sehingga menjadi :

# Set needed variables
:local username "user"
:local password "password"
:local hostname "mydomain_in_dyndns.net"
:global systemname [/system identity get name]

:if ($systemname  = "Site1" ) do= {
:set hostname "mydomain_in_dyndns.net"
}
:if ($systemname  = "Site2" ) do= {
:set hostname "mydomain1_in_dyndns.net"
}
:if ($systemname  = "Site3" ) do= {
:set hostname "mydomain2_in_dyndns.net"
}

:global dyndnsForce
:global previousIP

# print some debug info
:log info ("UpdateDynDNS: username = $username")
:log info ("UpdateDynDNS: password = $password")
:log info ("UpdateDynDNS: hostname = $hostname")
:log info ("UpdateDynDNS: 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 "UpdateDynDNS: currentIP = $currentIP"

# Remove the # on next line to force an update every single time - useful for debugging,
#  but you could end up getting blacklisted by DynDNS!

#:set dyndnsForce true

# 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 ("UpdateDynDNS: Dyndns update needed")
    :log info ("UpdateDynDNS: Dyndns Update Result: ".$result)
    :put ("Dyndns Update Result: ".$result)
#Erase name from firewall address list
/ip firewall address-list
:foreach i in=[find list="current_IP"] do=[remove $i]
#New adrdres to 
/ip firewall address-list add address=$currentIP list=current_IP
} else={
    :log info ("UpdateDynDNS: No dyndns update needed")
}

 

dengan perubahan script diatas maka perlu ada perubahan NAT, jika pada kondisi normal (ip public statis) ¬†kita memasukkan ip public isp ke Dst. Address maka disini cukup memasukkan address list “current_IP” ke dalam dst-address-list, seperti berikut :

chain=dstnat action=dst-nat to-addresses=192.168.10.1 to-ports=80 
     protocol=tcp dst-address-list=current_IP in-interface=wan dst-port=80

 

Setelah modifikasi script seperlunya maka server yang berada di belakang NAT dapat diakses dari luar dengan lancar.

Segitu dulu, kurang lebihnya mohon maaf …. semoga bermanfaat.

 

sumber script : wiki.mikrotik.com + forum.mikrotik.com

Related Search

Tags: #NAT

Anda sedang membaca artikel tentang Akses Mikrotik Dengan DynDNS. Silahkan baca artikel Be Log tentang Mikrotik lainnya. Semoga bermanfaat ...

Setup Storage Path The Dude di RB750G r3
Setup Storage Path The Dude di RB750G r3
The Dude merupakan tool yang cukup penting
Cara Membuat Certificates di MikroTik
Cara Membuat Certificates di MikroTik
Certificates yang dibuat pada Mikrotik ini dapat
Bypass Nawala dan Internet Positif di Mikrotik
Bypass Nawala dan Internet Positif di Mikrotik
Mungkin sudah banyak yang bahas, tapi ga

Top