DynDNS yangilash skripti NAT ortida MikroTik uchun

⏱ O‘qish vaqti: 7 daqiqa

Qisqacha

  • Script NAT ortidagi routerda joriy IP ni tekshiradi
  • IP o‘zgarganda DynDNS serveriga yangilash so‘rovini yuboradi
  • Natija email orqali administratorga yuboriladi
  • /file dyndns.txt ichida oxirgi IP saqlanadi

MikroTik routeringiz NAT ortida bo‘lsa, dinamik IP o‘zgarishi DynDNS xizmatini yangilashni to‘xtatadi. Ushbu skript IP o‘zgarishini avtomatik aniqlab, DynDNS serveriga yangilash so‘rovini yuboradi va natijani email bilan xabar beradi.

Talablar

  • RouterOSga administrator huquqi
  • DynDNS hisob qaydnomasi (username, password, hostname)
  • Routerda email sozlamalari o‘rnatilgan

Sozlash bosqichlari

1. O‘zgaruvchilarni sozlash

:local username "", :local password "", :local hostname "" va :local emailAddress "" o‘zgaruvchilari e’lon qilingan. Bu joylarga o‘z DynDNS hisobingiz ma’lumotlarini kiriting.

Kodda bu o‘zgaruvchilar keyinchalik URL yaratish va email jo‘natishda ishlatiladi.

Grateful thanks goes to "webasdf" on the forums (http://forum.mikrotik.com/viewtopic.php?p=171805#p171805) for this. I just edited it so that we can use the same identical script at every single one of our hotspots that are behind a Dynamic IP. The great ability of the original script by webasdf is that it uses DynDNS's html access method which means it will work behind a NAT'ed connection.

Script minimum Policy permissions required: read | write | test

:local username ""
:local password ""
:local hostname ""
:local emailAddress ""

:local url "dummy"
:local previousIP

:global dyndnsForce

:set dyndnsForce false

:log info ("UpdateDynDNS starts.")

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

# I have some doubt over the persistence of the global previousIP.
# This value should be stored in /dyndns.txt after the last update attempt,
# preceded by the status and a space.
# For status values see: https://help.dyn.com/remote-access-api/return-codes/

:if ([:len [/file find name=dyndns.txt]] > 0) do={
   :local ipfile [/file get dyndns.txt contents]
   :local ipstart ([find $ipfile " " -1] + 1)
   :local ipend [:len $ipfile]
   :set previousIP [:pick $ipfile $ipstart $ipend]
} else={
   :set previousIP "0.0.0.0"
}

# get the current IP address from the internet (in case of double-nat)
/tool fetch mode=http address="checkip.dyn.com" 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 "

2. Oldingi IP manzilini o‘qish

Skript /file find name=dyndns.txt mavjudligini tekshiradi. Agar fayl bo‘lsa, ichidan oxirgi IP manzilini :pick yordamida ajratib oladi, aks holda 0.0.0.0 ga tenglaydi.

Bu qiymat $previousIP o‘zgaruvchisiga saqlanadi va joriy IP bilan solishtiriladi.

Grateful thanks goes to "webasdf" on the forums (http://forum.mikrotik.com/viewtopic.php?p=171805#p171805) for this. I just edited it so that we can use the same identical script at every single one of our hotspots that are behind a Dynamic IP. The great ability of the original script by webasdf is that it uses DynDNS's html access method which means it will work behind a NAT'ed connection.

Script minimum Policy permissions required: read | write | test

:local username ""
:local password ""
:local hostname ""
:local emailAddress ""

:local url "dummy"
:local previousIP

:global dyndnsForce

:set dyndnsForce false

:log info ("UpdateDynDNS starts.")

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

# I have some doubt over the persistence of the global previousIP.
# This value should be stored in /dyndns.txt after the last update attempt,
# preceded by the status and a space.
# For status values see: https://help.dyn.com/remote-access-api/return-codes/

:if ([:len [/file find name=dyndns.txt]] > 0) do={
   :local ipfile [/file get dyndns.txt contents]
   :local ipstart ([find $ipfile " " -1] + 1)
   :local ipend [:len $ipfile]
   :set previousIP [:pick $ipfile $ipstart $ipend]
} else={
   :set previousIP "0.0.0.0"
}

# get the current IP address from the internet (in case of double-nat)
/tool fetch mode=http address="checkip.dyn.com" 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 "

3. Joriy IP manzilini aniqlash

/tool fetch yordamida checkip.dyn.com saytidan joriy IP olinadi, natija :find va :pick bilan ajratib, $currentIP ga saqlanadi.

Bu bosqich NAT ortidagi routerning internetga ko‘rsatayotgan IP manzilini aniqlash uchun zarur.

Grateful thanks goes to "webasdf" on the forums (http://forum.mikrotik.com/viewtopic.php?p=171805#p171805) for this. I just edited it so that we can use the same identical script at every single one of our hotspots that are behind a Dynamic IP. The great ability of the original script by webasdf is that it uses DynDNS's html access method which means it will work behind a NAT'ed connection.

Script minimum Policy permissions required: read | write | test

:local username ""
:local password ""
:local hostname ""
:local emailAddress ""

:local url "dummy"
:local previousIP

:global dyndnsForce

:set dyndnsForce false

:log info ("UpdateDynDNS starts.")

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

# I have some doubt over the persistence of the global previousIP.
# This value should be stored in /dyndns.txt after the last update attempt,
# preceded by the status and a space.
# For status values see: https://help.dyn.com/remote-access-api/return-codes/

:if ([:len [/file find name=dyndns.txt]] > 0) do={
   :local ipfile [/file get dyndns.txt contents]
   :local ipstart ([find $ipfile " " -1] + 1)
   :local ipend [:len $ipfile]
   :set previousIP [:pick $ipfile $ipstart $ipend]
} else={
   :set previousIP "0.0.0.0"
}

# get the current IP address from the internet (in case of double-nat)
/tool fetch mode=http address="checkip.dyn.com" 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 "

4. DynDNS yangilash va natijani emailga yuborish

Agar $currentIP oldingi IP dan farq qilsa yoki dyndnsForce true bo‘lsa, skript :log info bilan o‘zgarishni qayd etadi, URL yaratadi va /tool fetch bilan DynDNS serveriga yangilash so‘rovini yuboradi. So‘rov natijasi /file get dyndns.txt contents orqali o‘qilib, :log info va /tool e-mail send bilan emailga jo‘natiladi.

Natija faylida DynDNS javobi saqlanadi, shuningdek logda “Changing IP” yoki “No dyndns update needed” yozuvlari ko‘rinadi.

Grateful thanks goes to "webasdf" on the forums (http://forum.mikrotik.com/viewtopic.php?p=171805#p171805) for this. I just edited it so that we can use the same identical script at every single one of our hotspots that are behind a Dynamic IP. The great ability of the original script by webasdf is that it uses DynDNS's html access method which means it will work behind a NAT'ed connection.

Script minimum Policy permissions required: read | write | test

:local username ""
:local password ""
:local hostname ""
:local emailAddress ""

:local url "dummy"
:local previousIP

:global dyndnsForce

:set dyndnsForce false

:log info ("UpdateDynDNS starts.")

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

# I have some doubt over the persistence of the global previousIP.
# This value should be stored in /dyndns.txt after the last update attempt,
# preceded by the status and a space.
# For status values see: https://help.dyn.com/remote-access-api/return-codes/

:if ([:len [/file find name=dyndns.txt]] > 0) do={
   :local ipfile [/file get dyndns.txt contents]
   :local ipstart ([find $ipfile " " -1] + 1)
   :local ipend [:len $ipfile]
   :set previousIP [:pick $ipfile $ipstart $ipend]
} else={
   :set previousIP "0.0.0.0"
}

# get the current IP address from the internet (in case of double-nat)
/tool fetch mode=http address="checkip.dyn.com" 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 "

5. Skriptni ishga tushirish

RouterOS terminalida yangi script yaratib, yuqoridagi kodni joylashtiring. So‘ngra kerakli intervallar (masalan, har 5 daqiqada)da scriptni bajarilishini sozlang.

Tekshirish

Skript ishga tushgandan so‘ng logda «Changing IP» yoki «No dyndns update needed» yozuvlarini ko‘ring, /file get dyndns.txt contents ichida yangilangan IP ni tekshiring va belgilangan email manziliga natija xabar kelganligini tasdiqlang.

Diqqat:

  • Har safar yangilashni majburan bajarish (dyndnsForce=true) DynDNS provayderi tomonidan bloklanishga olib kelishi mumkin.
  • Skriptni bajarish uchun read | write | test ruxsatlari kerak.
  • Email jo‘natish uchun routerda to‘g‘ri SMTP sozlamalari bo‘lishi zarur.

Muammolar va yechimlar

IP o‘zgarmadi, lekin DynDNS yangilanmadi

Logda «No dyndns update needed» yozuvi borligini tekshiring; agar IP haqiqatan o‘zgarmagan bo‘lsa, bu normal holat.

Email kelmayapti

/tool e-mail send parametrlarini, ayniqsa SMTP server va autentifikatsiyani tekshiring.

Xulosa

Ushbu skript NAT ortidagi MikroTik routerda DynDNS yangilanishini avtomatlashtiradi, IP o‘zgarishini kuzatadi va administratorni email orqali xabardor qiladi. To‘g‘ri sozlanganidan so‘ng minimal qo‘shimcha aralashuv bilan ishlaydi.

Manba: Dynamic DNS Update Script for dynDNS behind NAT — mikrotiktool.github.io

0 0 ovozlar
Article Rating
Obuna boʻling
Xabar berish
guest

0 Comments
Eng avvalgi
Eng yangi Eng ko'p ovoz berilgan