D
D
Dmitry2015-11-25 10:34:01
Mikrotik
Dmitry, 2015-11-25 10:34:01

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

5 answer(s)
L
l0ser140, 2015-11-25
@seventh

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
}

E
EugeneT, 2015-11-25
@EugeneT

:local endLoc [:find $newIP "/" -1]

We are looking for a point, no?
:local endLoc [:find $newIP "." -one]

A
Alexander Romanov, 2016-01-20
@moneron89

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
}

I don't see the need for a global variable.
And don't forget to add a task to the scheduler.

L
LESHIY_ODESSA, 2015-11-25
@LESHIY_ODESSA

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"
}

1. You have an incomprehensible semicolon (;) at the end of the line with global
2. RouterOS is the same Linux, which means that PPPoE and pppoe are two different interfaces for it.
3. Some scripts may not work right away, set the maximum rights to .
/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

J
Joe Ree, 2016-11-12
@JoeRee

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
}

The scheduler is running every 10 minutes.
The script works, but, sometimes, the provider assigns a non-working IP again.
You have to wait those 10 minutes. until the next launch.
How to loop the script until the correct IP is obtained?
Or put a check every minute in the scheduler? In this case, will the memory of Mikrotik be clogged with frequent large logs?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question