Net_RouterOS
Net_RouterOS copied to clipboard
exec() problem
need your help again please: why this code doesn't work? in routerOS terminal is everything fine
try {
$util->exec(
'
:foreach v in=[find list="test"] do={
:do {
remove=$v;
} on-error={
:error ("failed to remove " . $v);
};
};
'
);
} catch (RouerOS\RouterErrorException $e) {
}
You can get error details by analyzing the exception. You can just echo the whole variable to get the whole thing.
In this case, I'm thinking it's remove $v not remove=$v. Idk how that went through in terminal. It shouldn't go there either.
Removing/disabling/enabling all items in a list can be made much more efficient if you just give the results of find to a single call. In scripting, that would be
remove [find list="test"]
And in API
$util->remove($util->find(RouerOS\Query('list', 'test')));
If you're doing more things in the exec than that one remove or the list is so big that it's transfer to PHP would be too much, you should use exec(). For smaller lists, it would be more efficient to do the 2 API calls of find and remove than the 3 calls of add, run and remove. Exactly where the line between "smaller" and "bigger" lists is, I don't know, as I haven't done benchmarks. Intuitively, I'd guess somewhere around 4k items maybe.
thank you, remove $v was the reason, and script is stable when :log warning(); is set
try {
$util->exec(
'
:foreach v in=[find list="'.$listNameToExport[$i].'"] do={
:do {
remove $v;
} on-error={
:log warning ("php: failed to remove " . $v);
};
};
'
);
};