System information

Manual:Scripting-examples
38
Strip netmask
This script is useful if you need ip address without netmask (for example to use it in firewall), but "/ip address
get [id] address" returns ip address and netmask.
Code:
:global ipaddress 10.1.101.1/24
:for i from=( [:len $ipaddress] - 1) to=0 do={
:if ( [:pick $ipaddress $i] = "/") do={
:put [:pick $ipaddress 0 $i]
}
}
Another much more simple way:
:global ipaddress 10.1.101.1/24
:put [:pick $ipaddress 0 [:find $ipaddress "/"]]
Resolve host-name
Many users are asking feature to use dns names instead of IP address for radius servers, firewall rules, etc.
So here is an example how to resolve RADIUS server's IP.
Lets say we have radius server configured:
/radius
add address=3.4.5.6 comment=myRad
And here is a script that will resolve ip address, compare resolved ip with configured one and replace if not equal:
/system script add name="resolver" source= {
:local resolvedIP [:resolve "server.example.com"];
:local radiusID [/radius find comment="myRad"];
:local currentIP [/radius get $radiusID address];
:if ($resolvedIP != $currentIP) do={
/radius set $radiusID address=$resolvedIP;
/log info "radius ip updated";
}
}
Add this script to scheduler to run for example every 5 minutes
/system scheduler add name=resolveRadiusIP on-event="resolver" interval=5m