Using API in Mikrotik

Dear All,
I found following thread for killing users using their username

http://forum.mikrotik.com/viewtopic.php?f=2&t=20542&start=0

The syntax is as follow:
ppp act rem [find name=user1]

for example: ppp active remove [find name=testuser]

I use Mikrotik API in VC++

mikrotik.Send(“/ppp/active/remove [find name=” + szUsername + “]”);
mikrotik.Send(“.tag=sss”, true);

The code in Mikrotik terminal works fine but nothing happend in VC++ code.

Any suggestion?

API is not a scripting language and, there is no command “find”. You have to use queries.
http://wiki.mikrotik.com/wiki/Manual:API#Queries

Dear support,
Thanks for your reply. I didn’t understand that is it possible to kill users using API or not?

If it’s possible, how?

Best regards,
omidh

Yes, it is possible

/ppp/active/remove
?name=testuser
=.tag=sss

Sorry for my stupid question. I have wrote following code using VC#, but nothing happens. Would you please help:

MK mikrotik = new MK(NASIP);

if (!mikrotik.Login(szUsername, szPassword))
{
       Console.WriteLine("Could not log in");
        mikrotik.Close();
        return;
}

mikrotik.Send("/ppp/active/remove");
mikrotik.Send("?name=testuser");
mikrotik.Send(".tag=sss", true);

I think, it should be like this:

/ppp/active/remove
=.id=testuser

Does not work.

well, it works at least with v3.28:

first,

/ppp/active/print
?name=testuser
=.proplist=.id

and then execute this with the result from previous command:

/ppp/active/remove
=.id=*VALUE*

I found user id after printing all online users, then use following:

mikrotik.Send("/ppp/active/remove");
mikrotik.Send("=.id=CBDCD3");
mikrotik.Send("=.tag=sss", true);

I also test =.id=*CBDCD3, =.id=CBDCD3, =.id=CBDCD3, but nothing happened.

Any suggestion???

recheck. as I said, http://forum.mikrotik.com/t/using-api-in-mikrotik/40715/8 works for me

p.s. if user logs in again, he has new .id

Thanks for your reply, If you have any code, would you please give me it?

I wrote following code:

mikrotik.Send("/ppp/active/print");
mikrotik.Send("?name=testuser");
mikrotik.Send("=.proplist=.id");
                        
mikrotik.Send("/ppp/active/remove");
mikrotik.Send("=.id=*CC7334");
mikrotik.Send("=.tag=sss", true);

About following line

mikrotik.Send(“=.id=*CC7334”);

Does it correct? =.id=*CC7334? or it needs another * after CC7334?

Thanks

“/ppp/active/print” is used to get exact value of .id

after that you should read the result, and use the value in “/ppp/active/remove” command

don’t think about the number of asterisks - use the value you received from first command

I have changed the code to the following and it’s now working well :slight_smile:

mikrotik.Send("/ppp/active/print");
mikrotik.Send("?name=" + szUsername);
mikrotik.Send("=.proplist=.id", true);

List<string> s = mikrotik.Read();

// ...
// Some code for retrieving ID from s
// szID = ....

mikrotik.Send("/ppp/active/remove");
mikrotik.Send("=.id=" + szID, true);

Thanks.