Why don’t you use the block inside on error to put the code for when CAPs is not available ?
From your script you have set CAPsMAN always true, so the IF will always reach.
Now if you put your code inside the on error, you will execute that part like when the IF would be false.
Like in this simple testcase.
In this case if the code is run when CAPsMAN is available, the IF is true and that part of code is executed.
While on error you will execute the portion of code inside the curly brackets.
as I don’t have CAPs I can’t try the true case, but you get the idea. In the IF put a condition that will be true if CAPs is there and inside the on-error block you will put the code for when CAPS is not there, like in this simple case the global variable is set to NOK when the code fails.
Armando.
I did a copy past error in test script above. The true stat is used as an override if you do not use the function.
Fixed the line. But this still does not work. If I add this to my script, nothing is run on a router without CAPsMAN
local CAPsMAN true
:do {
/caps-man registration-table find
:put "caps ok"
if ($CAPsMAN) do={
:local capsregistered ([/caps-man registration-table print count-only])
/caps-man interface
:local name
:local mac
# ignore all master interfaces
:foreach p in=[find where master-interface="none"] do={
:set name [get $p name]
:set mac [get $p radio-mac]
:local counter ([/caps-man registration-table print count-only where interface=$name])
:log info message="script=caps-man name=$name counter=$counter"
}
:log info message="script=caps-man capsregistered=$capsregistered"
}
} on-error={
:log info message="no cas"
}
Add it in bracket and cut and past to cli and I get lots of error on a router without CAPsMAN
{
local CAPsMAN true
:do {
/caps-man registration-table find
:put "caps ok"
if ($CAPsMAN) do={
:local capsregistered ([/caps-man registration-table print count-only])
/caps-man interface
:local name
:local mac
# ignore all master interfaces
:foreach p in=[find where master-interface="none"] do={
:set name [get $p name]
:set mac [get $p radio-mac]
:local counter ([/caps-man registration-table print count-only where interface=$name])
:log info message="script=caps-man name=$name counter=$counter"
}
:log info message="script=caps-man capsregistered=$capsregistered"
}
} on-error={
:log info message="no cas"
}
}
Well, playing little bit with your script even if you isolate it in a block or conditional if, the code is parsed anyway when running it on CLI, therefore all the missing commands for CAP wil produce several syntax errors.
One way to solve your issue could be to split your main code into a script that you will call only when a check is done before time.
What is important is to use a criteria that works when CAP is available: so I used in this example :if ([:len [/caps-man registration-table find]] > 0) but you have to test if the length of that output is > 0 just when CAP is available, otherwise the check script wont enter the IF case even if CAP is there.
So for example you can create a check script like this one:
:do {
:if ([:len [/caps-man registration-table find]] > 0) do={
/system script run script_ok
}
} on-error={/system script run script_nok}
Now I have created couple of dummy codes for both the OK and NOK cases, so it’s easy to see that the subsequent scripts is executed and its effect to change the global variable value.
So the script_ok code:
:global CAPsMAN true
and the script_nok for when CAPs is not there:
:global CAPsMAN false
Now if you place all your code into the script_ok, when the check script finds CAP available, it will call the subsequent script_ok.
:global CAPsMAN
:do {
if ($CAPsMAN) do={
/caps-man registration-table find
:local capsregistered ([/caps-man registration-table print count-only])
/caps-man interface
:local name
:local mac
# ignore all master interfaces
:foreach p in=[find where master-interface="none"] do={
:set name [get $p name]
:set mac [get $p radio-mac]
:local counter ([/caps-man registration-table print count-only where interface=$name])
:log info message="script=caps-man name=$name counter=$counter"
}
:log info message="script=caps-man capsregistered=$capsregistered"
}
} on-error={:set $CAPsMAN false}
Then a better solution to check and run the whole code can be adopted and you can see the best way for you.
Obviously if you copy/paste to CLI you will get errors because the code is parsed during reading, regardless the logic inside the code.
Armando.