Community discussions

MikroTik App
 
icu4ever
just joined
Topic Author
Posts: 6
Joined: Tue Aug 04, 2015 7:25 pm

Mikrotik API

Mon Sep 07, 2015 11:48 am

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"
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: Mikrotik API

Mon Sep 07, 2015 12:15 pm

please check RouterOS API manual. You will see that you cannot run scripts via API.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Mikrotik API

Tue Sep 08, 2015 12:12 am

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