Community discussions

MikroTik App
 
cicserver
Member
Member
Topic Author
Posts: 303
Joined: Sun Jul 24, 2011 12:04 pm

Multiple IF Statement match and action accordingly

Mon Apr 04, 2016 10:33 am

How can i match two statements and if BOTH equals to xxx, act accordingly.
Example:

If variable 1 and Variable 2 values in environment are equal to DOWN, then execute script.
I search forum and google, but found only one statement match examples.
 
User avatar
nz_monkey
Forum Guru
Forum Guru
Posts: 1919
Joined: Mon Jan 14, 2008 1:53 pm
Location: Over the Rainbow
Contact:

Re: Multiple IF Statement match and action accordingly

Mon Apr 04, 2016 11:07 am

if (condition) and (condition) do=
http://thebrotherswisp.com/ | Mikrotik MTCNA, MTCRE, MTCINE | Fortinet NSE7 | Extreme Networks ENA
 
cicserver
Member
Member
Topic Author
Posts: 303
Joined: Sun Jul 24, 2011 12:04 pm

Re: Multiple IF Statement match and action accordingly

Mon Apr 04, 2016 11:13 am

if (condition) and (condition) do=
Example:
variable stored in script environment are
var1 = up
var2 = down

I want if both variables value is equal to DOWN, then print "net is down"

:local var1 [/sys scr environment print value where name=status1];
:local var2 [/sys scr environment print value where name=status2];

:if $var1 and $var2 = DOWN do={
    :put "down";
else
:put "still working"
}
something like abobe
 
User avatar
aacable
Member
Member
Posts: 434
Joined: Wed Sep 17, 2008 11:58 am
Location: ISLAMIC Republic of PAKISTAN
Contact:

Re: Multiple IF Statement match and action accordingly

Mon Apr 04, 2016 11:53 am

How can i match two statements and if BOTH equals to xxx, act accordingly.
Example: If variable 1 and Variable 2 values in environment are equal to DOWN, then execute script. I search forum and google, but found only one statement match examples.
Try this.
:local var1 [/system script environment get [/system script environment find name="enviornment-1"] value];
:local var2 [/system script environment get [/system script environment find name="enviornment-2"] value];
:if (($var1 = "DOWN") && ($var2 = "DOWN")) do={ 
:log info "DOWN"
} else={
:log info "UP"
}
}
}
_____________
Regard's

Syed Jahanzaib
Web: http://aacable.wordpress.com
Email: aacable [at] hotmail.com
 
cicserver
Member
Member
Topic Author
Posts: 303
Joined: Sun Jul 24, 2011 12:04 pm

Re: Multiple IF Statement match and action accordingly

Tue Apr 05, 2016 11:55 am

thx, it worked ok.
 
profound
just joined
Posts: 2
Joined: Wed Apr 04, 2018 1:11 pm

Re: Multiple IF Statement match and action accordingly

Wed Sep 23, 2020 10:52 am

Thanks for the post.

I've extended it slightly, to log events, and also only change the fan if the temperature warrants the change, and if the fan is not already being used. This way you can have the main fan faster, and a slower, quieter auxilary fan in place, for when the device is adequately cooled. I have this script scheduled (/system script run FanControl) to run every 15 seconds.
:if (([/system health get cpu-temperature]> 60) && ([/system health get use-fan]="auxiliary")) do={
  [/system health set use-fan="main"];
  log info "CPU is hot, switching to main fan";
}
:if (([/system health get cpu-temperature]< 55) && ([/system health get use-fan]="main")) do={
  [/system health set use-fan="auxiliary"];
  log info "CPU is cool, switching to auxiliary fan";
}
 
User avatar
aacable
Member
Member
Posts: 434
Joined: Wed Sep 17, 2008 11:58 am
Location: ISLAMIC Republic of PAKISTAN
Contact:

Re: Multiple IF Statement match and action accordingly

Wed Sep 23, 2020 12:35 pm

Thanks for the post.

I've extended it slightly, to log events, and also only change the fan if the temperature warrants the change, and if the fan is not already being used. This way you can have the main fan faster, and a slower, quieter auxilary fan in place, for when the device is adequately cooled. I have this script scheduled (/system script run FanControl) to run every 15 seconds.
:if (([/system health get cpu-temperature]> 60) && ([/system health get use-fan]="auxiliary")) do={
  [/system health set use-fan="main"];
  log info "CPU is hot, switching to main fan";
}
:if (([/system health get cpu-temperature]< 55) && ([/system health get use-fan]="main")) do={
  [/system health set use-fan="auxiliary"];
  log info "CPU is cool, switching to auxiliary fan";
}
Thx for the contribution.
_____________
Regard's

Syed Jahanzaib
Web: http://aacable.wordpress.com
Email: aacable [at] hotmail.com
 
User avatar
rushlife
Member Candidate
Member Candidate
Posts: 171
Joined: Thu Nov 05, 2015 12:30 pm
Location: czech republic

Re: Multiple IF Statement match and action accordingly

Fri Apr 09, 2021 8:21 am

How can i match two statements and if BOTH equals to xxx, act accordingly.
Example: If variable 1 and Variable 2 values in environment are equal to DOWN, then execute script. I search forum and google, but found only one statement match examples.
Try this.
thx a lot, awesome
I am using it for my own watchdog, thx bro
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 2161
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Multiple IF Statement match and action accordingly

Fri Apr 09, 2021 12:00 pm

A cleaned up version.
No need for ; at the end of line, and removed extra parentheses.

{
	:local var1 "DOWN"
	:local var2 "DOWN"
	:if ($var1 = "DOWN" && $var2 = "DOWN") do={ 
		:put "Both are DOWN"
	} else={
		:put "UP"
	}
}
You can any of these: and = &&.
And this is the same:or = ||
 
Try Splunk> to monitor your MikroTik Router(s). Look at this page in how to set it up.

MikroTik->Splunk
 
 
mehdi2000
just joined
Posts: 8
Joined: Fri Aug 19, 2016 8:54 am

Re: Multiple IF Statement match and action accordingly

Mon May 10, 2021 10:28 pm

Hi
it's run in terminal perfectly but not work in script!!!
why???
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 3870
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Multiple IF Statement match and action accordingly

Tue May 11, 2021 12:29 am

what you try to run?
paste here
I'm Italian, not English. Sorry for my imperfect grammar.
 
mehdi2000
just joined
Posts: 8
Joined: Fri Aug 19, 2016 8:54 am

Re: Multiple IF Statement match and action accordingly

Tue May 11, 2021 10:52 pm

:if (([$percentage] >= 99) or ($EXDate = 1)) do={
/queue simple set $i max-limit=128k/128k;
}
:if (([$percentage] < 99) and ($EXDate = 0)) do={ /queue simple set $i max-limit="$maxUpload/$maxDownload"}

RB : 951Ui-2HnD
v : 6.48.2
in terminal is ok but in script no.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 3870
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Multiple IF Statement match and action accordingly

Wed May 12, 2021 4:52 am

It's only a fragment, it's not related with previous post, and without publish anyting you ask why not work???
I'm Italian, not English. Sorry for my imperfect grammar.
 
mehdi2000
just joined
Posts: 8
Joined: Fri Aug 19, 2016 8:54 am

Re: Multiple IF Statement match and action accordingly

Wed May 12, 2021 7:59 am

It's not fragmented .
I wrote this code from aacable code and I don't know why is not working in script ,but work perfectly in terminal
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6254
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Multiple IF Statement match and action accordingly

Wed May 12, 2021 9:14 am

If it is not a fragment, then it is not supposed to work. Script doesn't know anything about all those undefined variables you are trying to use.

Who is online

Users browsing this forum: No registered users and 40 guests