Community discussions

MikroTik App
 
omidh
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Tue Oct 27, 2009 4:00 pm
Location: Iran
Contact:

Using API in Mikrotik

Mon Sep 13, 2010 9:36 am

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

http://forum.mikrotik.com/viewtopic.php ... 42&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?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7053
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Using API in Mikrotik

Mon Sep 13, 2010 9:40 am

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
 
omidh
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Tue Oct 27, 2009 4:00 pm
Location: Iran
Contact:

Re: Using API in Mikrotik

Mon Sep 13, 2010 3:14 pm

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
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7053
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Using API in Mikrotik

Mon Sep 13, 2010 3:23 pm

Yes, it is possible
/ppp/active/remove
?name=testuser
=.tag=sss
 
omidh
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Tue Oct 27, 2009 4:00 pm
Location: Iran
Contact:

Re: Using API in Mikrotik

Mon Sep 13, 2010 3:49 pm

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);
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Using API in Mikrotik

Mon Sep 13, 2010 7:17 pm

I think, it should be like this:
/ppp/active/remove
=.id=testuser
 
omidh
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Tue Oct 27, 2009 4:00 pm
Location: Iran
Contact:

Re: Using API in Mikrotik

Mon Sep 13, 2010 8:07 pm

Does not work.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Using API in Mikrotik

Tue Sep 14, 2010 3:09 am

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*
 
omidh
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Tue Oct 27, 2009 4:00 pm
Location: Iran
Contact:

Re: Using API in Mikrotik

Tue Sep 14, 2010 9:03 am

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???
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Using API in Mikrotik

Tue Sep 14, 2010 9:21 am

recheck. as I said, http://forum.mikrotik.com/viewtopic.php ... 73#p226873 works for me

p.s. if user logs in again, he has new .id
 
omidh
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Tue Oct 27, 2009 4:00 pm
Location: Iran
Contact:

Re: Using API in Mikrotik

Tue Sep 14, 2010 8:54 pm

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
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Using API in Mikrotik

Tue Sep 14, 2010 11:22 pm

"/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
 
omidh
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Tue Oct 27, 2009 4:00 pm
Location: Iran
Contact:

Re: Using API in Mikrotik

Fri Sep 17, 2010 9:41 am

I have changed the code to the following and it's now working well :)
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.

Who is online

Users browsing this forum: eworm, GoogleOther [Bot], Kraken2k, svh79 and 109 guests