I have a couple of scripts that’s triggered by netwatch that quite simply turns on a mangle rule to tag packets to take a different route if one of my Wan connections drops.
The ones “BEup” and “BEdown” function as expected , the other 2 do not seem to actually do anything when run regardless of if they’re run by netwatch or manually by me.
Yet copying the content of the script and running it in the CLI works fine
Don’t use numbers in scripts. Numbers are only for interactive use. Instead, set a comment on the mangle rule, and find it programmatically in your script via the comment, and then disable/enable the found item.
By way of example:
/ip firewall mangle
add chain=prerouting action=accept comment="this is the rule"
The below in a script disables that rule:
/ip firewall mangle { disable [find comment="this is the rule"] };
There’s that, though it’s not disallowed. The real reason as I understand it that numbers might not work is that the numbers are generated by the CLI on the fly for interactive reference.
If you run this on the CLI:
:put [/ip firewall mangle find comment="this ist the rule"]
you’ll find you get back an odd looking value such as “*1”. That’s the actual reference to the configuration item, it’s a pointer in the classic sense. When you issue a “print” command the router dynamically generates numbers for humans to use that map to those pointers, so those numbers are then good for a human to use. A scheduled script doesn’t run in that environment, so the numbers haven’t been generated. People used to “print” in scripts first, and then use numbers. If you simply use a find command you’re guaranteed to get the correct pointer to the configuration object. I tend to use find commands in all my scripts I run manually in the CLI as well, just to get around re-ordering issues. It isn’t much more to type, and unlike the numbers it is entirely unambiguous. Unless you have HUGE lists of configuration items to traverse the hit on resources to find the item is negligible - and if you have that many mangle rules you’re having CPU issues, anyway, just during packet processing of those rules. Not using find commands for resource reasons in my opinion would be very premature optimization.