Community discussions

MikroTik App
 
lukkes
Member Candidate
Member Candidate
Topic Author
Posts: 177
Joined: Mon Jun 16, 2008 2:12 am
Location: Venezuela
Contact:

exit or break a loop statement

Fri May 28, 2010 9:48 pm

Hello everybody, I'm making a script that ping more than 3 hosts, if first fail then ping second and so on, but if some of the hosts it's up then the loop should terminate and dont ping the rest of the servers, in mikrotik language there is some break or exit order than can make this possible i need break or exit the loop inmediatly when some host respond to ping. thanks


***** this works, but somebody has another idea..? *****
:local continue true;
:local counter 0;
:while ($continue) do={
   :log info $continue
   :set counter ($counter + 1);
   :log info $counter;
   :if ($counter=50) do={:set continue false;}
}
:log info "end"
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7044
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: exit or break a loop statement

Mon May 31, 2010 10:27 am

Currently there is no "break" or "exit" in RouterOS scripting.
You will have to use "while" loop just like in your example.
 
lukkes
Member Candidate
Member Candidate
Topic Author
Posts: 177
Joined: Mon Jun 16, 2008 2:12 am
Location: Venezuela
Contact:

Re: exit or break a loop statement

Mon May 31, 2010 2:30 pm

Right, Thanks, the problem was resolved with while. more job to do but the same result.
 
thantoldo
just joined
Posts: 22
Joined: Tue Apr 10, 2012 10:08 pm

Re: exit or break a loop statement

Mon Jun 10, 2013 10:59 am

Any plans to add loop "break" or "exit" in RouterOS scripting?
 
User avatar
webpagetech
newbie
Posts: 42
Joined: Fri Mar 01, 2013 8:42 pm
Location: TX, United States
Contact:

Re: exit or break a loop statement

Wed Nov 06, 2013 5:52 am

+1
This would be useful... The while continue will work if you want to break at the end of the while loop but for the loop to actually stop, processing has to make it back to the ":while ($continue) do={". This means that if you set set continue to false half way though the while loop the rest of the while loop still processes.

A way around this is to have every statement in the while loop enclosed in a ":if ( $continue = true ) do={" block.

If you have a large while loop this makes allot of extra code just to do a break; :( .

Thanks Mikrotik team for your work in 6.2 to make functions much cleaner. Scripting just got a little more fun :D.
 
User avatar
juliokato
Member Candidate
Member Candidate
Posts: 228
Joined: Mon Oct 26, 2015 4:27 pm
Location: Brazil

Re: exit or break a loop statement

Mon Apr 10, 2017 9:22 pm

+1.
 
User avatar
glee
just joined
Posts: 5
Joined: Fri Aug 18, 2017 5:44 pm

Re: exit or break a loop statement

Fri Aug 18, 2017 5:51 pm

I found one optional solution - use error, this will stop script.
Wiki:
error :error <output> Generate console error and stop executing the script
 
Bdaisnub
just joined
Posts: 1
Joined: Wed Feb 14, 2018 5:19 pm

Re: exit or break a loop statement

Wed Feb 14, 2018 5:22 pm

I found one optional solution - use error, this will stop script.
Wiki:
error :error <output> Generate console error and stop executing the script
Thank you!
 
hackclub
newbie
Posts: 26
Joined: Thu Dec 12, 2013 7:28 am

Re: exit or break a loop statement

Tue Feb 27, 2018 9:41 pm

+1
Thanks ...
 
User avatar
yesurbius
just joined
Posts: 4
Joined: Mon Oct 16, 2017 8:10 am
Location: Edmonton, Canada

Re: exit or break a loop statement

Fri May 25, 2018 7:41 pm

I am not certain how to use "while ($continue)" loop in the same manner as a foreach loop.

I'm trying to add a rule before the first drop action in the filters.. maybe there is a better way to do this ...

Code: Select all

foreach filterrule in [/ip firewall filter find where action=drop] do={
/ip firewall filter add action=accept chain=input comments="Remote Administration" dst-port=8291 protocol=tcp src-address-list=REMOTE_Admin
break
}
Of course that doesn't work...
 
User avatar
yesurbius
just joined
Posts: 4
Joined: Mon Oct 16, 2017 8:10 am
Location: Edmonton, Canada

Re: exit or break a loop statement

Fri May 25, 2018 7:47 pm

Nevermind ... I figured it out an alternative method using place-before and :pick/:find

Code: Select all

/ip firewall filter add action=accept chain=input comments="Remote Administration" dst-port=8291 protocol=tcp src-address-list=REMOTE_Admin place-before=[:pick [find action=drop] 0]
Will leave my post just incase someone else is looking for same information
 
nomis1234
just joined
Posts: 2
Joined: Fri Feb 21, 2020 1:18 pm

Re: exit or break a loop statement

Fri Feb 21, 2020 2:21 pm

With "on-error" it is now possible to break a for/foreach loop and continue with the rest of the script.
In that case you even have the possibility to execute extra instructions in case the loop is exited prematurely.
:do {
   :foreach i in 1,2,3,4,5,6 do={
      :if ($i = 4) do={
         :put "break?";
#        This is generating a run-time error:
         /break;
      }
      :if ($i = 2) do={
         :do {
            :put "break?";
            /break;
         } on-error={
            :put "error handled. do not break.";
         }
      }
      :put $i;
   }
} on-error={
   :put "break!";
}
Output:
1
break?
error handled. do not break.
2
3
break?
break!

Who is online

Users browsing this forum: No registered users and 17 guests