Answer the question
In order to leave comments, you need to log in
How to determine the first IP octet in the Mikrotik script?
Hello dear.
Mikrotik connects via PPPoE, gets an honest dynamic address. But we do not like its behavior when the address starts with 213 - for some reason it is not accessible from the outside from this network. Therefore, the task is to write a script that will determine the first IP octet and if it is equal to 213, put a PPPoE connection and raise it again after 30 seconds.
The general process is clear, except for the main one - how to extract this first octet in the script from the address? Stupidly slightly modified another script, please help me finish it. Especially in the most incomprehensible part about parsing.
:global newIP [/ip address get [find interface="PPPoE"] address];
# parse the current IP result
:local resultLen [:len $newIP]
:local startLoc 0
:local endLoc [:find $newIP "/" -1]
:global currentIP [:pick $newIP $startLoc $endLoc]
:if ($currentIP = 213) do={
/interface pppoe-client disable PPPoE
:delay 30
/interface pppoe-client enable PPPoE
}
Answer the question
In order to leave comments, you need to log in
For the same network mask is.
:global newIP [/ip address get [find interface="PPPoE"] address];
:if ($newIP in 213.0.0.0/8) do={
/interface pppoe-client disable PPPoE
:delay 30
/interface pppoe-client enable PPPoE
}
:local endLoc [:find $newIP "/" -1]
:local endLoc [:find $newIP "." -one]
Several solutions have already been presented. Another solution in one line:
:local "current-ip" [:pick [/ip address get [find interface =PPPoE] address ] 0 3]
:if ($currentIP = 213) do={
/interface pppoe-client disable PPPoE
:delay 30
/interface pppoe-client enable PPPoE
}
Then like this:
:global "out-interface" "pppoe"
:local "current-ip" [/ip address get [find interface=$"out-interface"] address]
:local "result" [:find $current-ip "." -1]
:if ($result = 213) do={
/interface pppoe-client disable $"out-interface"
:delay 30
/interface pppoe-client enable $"out-interface"
}
/system scheduler
add disabled=no interval=1m name=myscript on-event="/system script run myscript" policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api start-time=startup
I included a similar script, but with the reverse logic - there is access to the personal forum from the outside only when the IP starts with 212:
:local newIP [:pick [/ip address get [find interface ="RT"] address ] 0 3]
:if ($newIP != 212) do={
/interface pppoe-client disable RT
:delay 5
/interface pppoe-client enable RT
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question