📜 ⬆️ ⬇️

Script automatic update DDNS for No-IP

Having read a lot of manuals and examples of writing a script for Mikrotik to work with the noip.com service, they did not manage to find a ready-made solution.
What did not suit in other manuals, for example, here :
when writing it is necessary directly in the text of the script to specify the name of the interface from which it will receive the external IP address, and what if there are two, three or ten interfaces?
In my case there are 2 channels: pppoe-client and ethernet without a password, but with a dynamically derived address ...

Also in the routs there are 2 entries with “dst. address "equal to" 0.0.0.0/0 "with distances" 1 "and" 2 ", respectively.

image

First you need to sort through all the routes, finding records with “dst. address "equal to" 0.0.0.0/0 ", then check the interface activity (: if ([get $ counter active] = true)).
Thus, we get the name of the active interface and store it in the local variable “activeInterface”.
')
:local activeInterface; /ip route { :local gwintarray; :local counter; :local intfinder; :foreach counter in=[find dst-address=0.0.0.0/0] do={ :if ([get $counter active] = true) do={ :set $activeInterface [get $counter gateway]; } } } 


After that, we need to sort through all the entries in the table / ip address and find one with the name of the desired interface.
The resulting address will contain a subnet mask, which we immediately cut off.

 :local activeAddress; /ip address { :set $activeAddress [get [find interface=$activeInterface] address]; :set $activeAddress [:pick $activeAddress 0 [:find $activeAddress "/"]]; } 


image

After that, we will write the data to connect to the NO-IP service:

 :local ddnsuser "your_no-ip_user"; :local ddnspass "your_no-ip_pass"; :local ddnshost "hostname.no-ip.org"; :local str "/nic/update?hostname=$ddnshost&myip=$activeAddress"; /tool fetch url="http://dynupdate.no-ip.com/$str" mode=http user=$ddnsuser password=$ddnspass \ dst-path=("/ServiceDNS.".$ddnshost); 


Where:
ddnsuser - login in the system NO-IP
ddnspass - password in the system NO-IP
ddnshost - the domain name for which you want to update the IP address

And pass the information to the service by downloading the file with the answer:

 :local str "/nic/update?hostname=$ddnshost&myip=$activeAddress"; /tool fetch url="http://dynupdate.no-ip.com/$str" mode=http user=$ddnsuser password=$ddnspass \ dst-path=("/ServiceDNS.".$ddnshost); 


After that, we wait 2 seconds and display the contents of the file (the status of the IP address update), and then delete it.

 :delay 2; :local str [/file find name="ServiceDNS.$ddnshost"]; :log info [/file get $str contents]; /file remove $str 


ATTENTION!!! The manual uses the global variable " previousIP ", which remembers the previous IP address .
If the address of the global variable matches the newly received one, the script will not update anything on the service.

MINUS of this method: go to the site noip.com and manually change the IP-address. A script microtic does not track. So in my example, this variable is simply excluded.

The full script code can be found here.
 #    # Get interface name :local activeInterface; /ip route { :local gwintarray; :local counter; :local intfinder; :foreach counter in=[find dst-address=0.0.0.0/0] do={ :if ([get $counter active] = true) do={ :set $activeInterface [get $counter gateway]; } } } #  IP    # Get IP-address of actived interface :local activeAddress; /ip address { :set $activeAddress [get [find interface=$activeInterface] address]; :set $activeAddress [:pick $activeAddress 0 [:find $activeAddress "/"]]; } #    NO-IP # No-IP User account info :local ddnsuser "your_no-ip_user"; :local ddnspass "your_no-ip_pass"; :local ddnshost "hostname.no-ip.org"; #   # Updating data on NO-IP :local str "/nic/update?hostname=$ddnshost&myip=$activeAddress"; /tool fetch url="http://dynupdate.no-ip.com/$str" mode=http user=$ddnsuser password=$ddnspass \ dst-path=("/ServiceDNS.".$ddnshost); #  2  # Wait 2 seconds :delay 2 #          # Displays information about the status of the update and delete the downloaded file :local str [/file find name="ServiceDNS.$ddnshost"]; :log info [/file get $str contents]; /file remove $str 



Below is information for beginners.
To add a script to Mikrotik, open the “System”> “Scripts” menu and add a new script by clicking on the “plus” icon and call the script, for example, “ update-ddns ”

image

It remains to add the script startup rule to the scheduler. To do this, go to “System”> “Scheduler” and click on the familiar “plus”.
In the name we indicate the name of the rule in the scheduler.
In the " On Event " field we indicate the name of our script - " update-ddns ", set the launch rules and click " OK ".

In my case, the script is run every 61 seconds.

image

PS: The script was tested on Mikrotik RB850Gx2 (powerpc core) with firmware version 6.33.5.
It also runs on Mikrotik RB450G and RB951G-2HnD with firmware version 6.33.5 without any problems.

That's all!

Source: https://habr.com/ru/post/275761/


All Articles