Mikrotik API

Hi everyone,

i wanna run this mikrotik script
" :foreach i in=[/tool user-manager session find ] do {/tool user-manager session remove $i;} " into c# with api

i got this error
[0]: “!trap=category=0=message=no such command or directory (foreach i in=[)”
[1]: “!trap=message=no such command prefix”
[2]: “!done”

please check RouterOS API manual. You will see that you cannot run scripts via API.

FWIW, this is a bad way to remove all sessions, even in scripting alone.

Using “find” inside the remove itself is far more compact, as well as more efficient:

/tool user-manager session remove [find]

The only time using :foreach is necessary is if your criteria can’t be covered by the “where” inside the “find”.

To do the above (not using :foreach) using the API, you need to execute the inner command, and pass it’s results to the outer command.

So f.e.

/tool/user-manager/session/find

!done
=ret=*1,*2,*3

/tool/user-manager/session/remove
=numbers=*1,*2,*3

!done