Friday, August 14, 2015

Update Dynamic DNS di MikroTik Untuk Dyndns, No-IP atau ChangeIP

MikroTik dapat kita memanfaatkan fitur script dan scheduler untuk update secara otomatis IP Dynamic DNS yang kita muliki.

Masukan script berikut ini di Winbox => System => Scripts, kolom “Name: update_ip_ddns” dan pada kolom “Source:” isi dengan script dibawah ini :

#**********************************************************************
# Update Dynamic DNS di MikroTik
# Ubah dengan infomasi akun anda, username/password/hostname DDNS
#**********************************************************************

:local username "username_anda"
:local password "password_anda"
:local hostname "hostname.anda"
 
# 2 pilihan untuk menangkap IP public anda "http" atau "iface"
# - http: akan mencari IP public yang digunakan (jika dibelakang NAT modem ADSL)
# - iface: akan menggunakan ip public yang di set di interface WAN mikrotik

:local discoverBy "http"

# nama interface WAN yang digunakan IP public (jika pilihan discoverBy = iface)

:local iface "nama_interface_wan_anda"

# pilih layanan Dynamic DNS yang digunakan: "dyndns", "noip", and "changeip"

:local service "changeip"

# Schedule(hari) untuk maksa update jika IP tidak berubah-ubah (agar DDNS tetap aktif)
:local forceUpdate 15

#**********************************************************************
# Jangan di rubah jika tidak mengerti yang akan di ubah
#**********************************************************************

:local force
:global lastUpdate
:local currentIP

:if ($discoverBy="http") do={
   /tool fetch mode=http address="checkip.dyndns.org" src-path="/" dst-path="/dyndns.checkip.html"
   :local result [/file get dyndns.checkip.html contents]
   :local resultLen [:len $result]
   :local startLoc [:find $result ": " -1]
   :set startLoc ($startLoc + 2)
   :local endLoc [:find $result "</body>" -1]
   :set currentIP [:pick $result $startLoc $endLoc]
} else={
   :set currentIP [ /ip address get [find interface=$iface disabled=no] address ]
   :for i from=( [:len $currentIP] - 1) to=0 do={
      :if ( [:pick $currentIP $i] = "/") do={ :set currentIP [:pick $currentIP 0 $i] }
   }
}

:local resolvedIP [:resolve $hostname]
:local date [ /system clock get date ]
:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
:local month [ :pick $date 0 3 ]; :local day [ :pick $date 4 6 ]; :local year [ :pick $date 7 11 ];
:local mm ([ :find $months $month -1 ] + 1);
:if ($mm < 10) do={ :set month ("0" . $mm); } else={ :set month $mm; }
:set date ($year . $month . $day);

:if ([ :typeof $lastUpdate ]=[:nothing] || (($date-$lastUpdate) >= $forceUpdate  && $forceUpdate > 0)) do={
   :set force true
}

:put ("Current IP: $currentIP ($discoverBy), Last update: $lastUpdate")

# Determine if dyndns update is needed
:if (($currentIP != $resolvedIP) || ($force = true)) do={
   
    :if ($service = "dyndns") do={
       /tool fetch user=$username password=$password mode=http address="members.dyndns.org" \
            src-path="/nic/update?hostname=$hostname&myip=$currentIP" dst-path="/output.txt"
    }
    :if ($service = "noip") do={
       /tool fetch user=$username password=$password mode=http address="dynupdate.no-ip.com" \
            src-path="/nic/update?hostname=$hostname&myip=$currentIP" dst-path="/output.txt"
    }
    :if ($service = "changeip") do={
       /tool fetch user=$username password=$password mode=http address="nic.changeip.com" \
            src-path="/nic/update?hostname=$hostname&myip=$currentIP" dst-path="/output.txt"
    }
    :local result [/file get output.txt contents]
    :log info ("dynamic-dns-updater: Service = $service, Hostname = $hostname")
    :log info ("dynamic-dns-updater: CurrentIP = $currentIP, Resolved IP = $resolvedIP")
    :log info ("dynamic-dns-updater: Update result: ".$result)
    /ip dns cache flush
    :set lastUpdate $date
}

Script tersebut diatas mempunyai fungsi :

Update otomatis IP Dynamic DNS untuk layanan Dyndns, No-IP atau ChangeIP.
Opsi tangkap IP public dibelakang NAT atau tidak.
Log tanggal update, dan perubahan IP.
Otomatis menghapus record DNS internal (flush DNS mikrotik) agar hostname DDNS terupdate dengan IP barunya.

Kemudian buat Schedule agar update DDNS setiap 5 Menit

/system scheduler
add disabled=no interval=5m name="Update DDNS 5 menit" on-event=update_ip_ddns policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api start-date=aug/14/2015 start-time=11:24:00

Sekarang tinggal di cek apakah sudah bekerja dengan baik atau tidak dengan melihat IP address yang terupdate di situs DDNS juga bisa dengan mengakses hostname yang sudah ditentukan.

0 comments:

Post a Comment