I try to write a little programm (C#) which could disable and enable clients in /ppp/secrets with API.
Now I learn a syntax of commants.
How can I disable and enable clients by name?
I try:
mikrotik.Send(“/ppp/secret/disable=name=”);
mikrotik.Send(“/ppp/secret/disable=find=name=”);
mikrotik.Send(“/ppp/secret/disable=”);
but it isn’t work?
Could anybody help me?
Each API word is a separate call to Send(), and you can’t use other commands inside a command - you must instead call the first (inner) command, store its result, and supply it to the second (outer) command. Also, the last API word should include a second argument with a value of “true”, to implicitly make the client send an empty API word…
So f.e.
mikrotik.Send("/ppp/secret/disable");
mikrotik.Send("=name=<name>", true);
Or I think maybe this C# client in particular, you had to do the opposite - supply false when you don’t want to end the command, e.g.
mikrotik.Send("/ppp/secret/disable", false);
mikrotik.Send("=name=<name>");
Unfortunately it isn’t work too…
My programm (very simple):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Threading.Tasks;
class MK (from Mikrotik Wiki)
namespace MikroTikAPI
{
class Program
{
static void Main(string[] args)
{
string User;
string[] array1 = new string[8];
MK mikrotik = new MK("10.10.0.1");
Console.Write("\nСоединение с устройством установлено");
if (mikrotik.Login("arey", "ххххххххх"))
{
mikrotik.Send("/ppp/secret/disable");
mikrotik.Send("=find");
mikrotik.Send("=name=demiurg",true);
Console.Write("\nОтключено");
Console.ReadKey(true);
}
}
}
}
I tried:
mikrotik.Send("/ppp/secret/disable", false);
mikrotik.Send("=find", false);
mikrotik.Send("=name=demiurg");
mikrotik.Send("/ppp/secret/disable");
mikrotik.Send("=name=<name>", true);
mikrotik.Send("/ppp/secret/disable", false);
mikrotik.Send("=name=<name>");
When I start the programm in the logs I see:
user arey logged in from 10.10.0.2 via api
user arey logged out from 10.10.0.2 via api
And that’s all.
What you’re sending is equivalent to the CLI
/ppp secret disable find="" name="demiurg"
Try doing that, and you’ll get an error too.
If you open up CLI, and type
/ppp secret disable ?
(or in general, just type “?” after commands)
you’ll see the arguments they accept. In the case of disable, that’s the argument “numbers”, which accepts the ID or name of item(s) to disable, so in your case:
/ppp secret disable numbers="demiurg"
or with API:
mikrotik.Send("/ppp/secret/disable");
mikrotik.Send("=numbers=demiurg",true);
Thank you very much! It works!
With CLI I use commands:
/ppp secret
disable [find name="demiurg"]
And with API it isn’t work…
Like I said, you can’t have a command inside a command (the “[” and “]” parts).
If you need to use “find”, you can run it separately, store its result, and supply the result as a value for the “numbers” argument.
Unfortunately, I have 2 new problems)
1:
I have to add a new address into address list.
CLI: /ip firewall address-list add address=10.11.0.11 comment=2-404 list=BlockedSecret
API (how I tried):
mikrotik.Send("/ip/firewall/address-list/add");
mikrotik.Send("=address=10.11.0.11");
miktotik.Send("=comment=2-404");
mikrotik.Send("=list=BlockedList", true);
mikrotik.Send("/ip/firewall/address-list/add");
mikrotik.Send("=address=10.11.0.11=comment=2-404=list=BlockedList", true);
How I should do it?
2:
Using find (print in API).
I have to search id of address in address list, for example, and remove it.
CLI: /ip/firewall/address-list remove [find address=10.11.0.11 and list=BlockedSecret]
I can’t use in API, so I save a result from print into the string array and then get the id. Bit I can’tunderstand how I should write complex condition in API. I tried:
mikrotik.Send("/ip/firewall/address-list/print");
mikrotik.Send("?address=10.11.0.1=?list=BlockedSecret", true);
mikrotik.Read();
Thanks for your help.
-
The first way is correct. If that doesn’t work, try adding “false” on all words except the last (as opposed to what you do now - adding “true” to only the last).
-
In addition to doing the two checks, you also need to add an “and” operator, or else you’re “or”-ing the two values, so:
mikrotik.Send("/ip/firewall/address-list/print");
mikrotik.Send("?address=10.11.0.1");
mikrotik.Send("?list=BlockedSecret");
mikrotik.Send("?#&", true);
Kinda an old thread, but need to correct you here to limit future confusion.
In the C# class seen here: http://wiki.mikrotik.com/wiki/API_in_C_Sharp (specific revision)
Writing any bool as the second parameter will end the sentence, doesn’t matter if it is true or false.
I suggest changing the Send method to the following:
public void Send(string co)
{
byte[] bajty = Encoding.ASCII.GetBytes(co.ToCharArray());
byte[] velikost = EncodeLength(bajty.Length);
_connection.Write(velikost, 0, velikost.Length);
_connection.Write(bajty, 0, bajty.Length);
}
public void Send(string co, bool endsentence)
{
Send(co);
if (endsentence)
EndSentence();
}
public void EndSentence()
{
_connection.WriteByte(0);
}
You don’t have to use the EndSentence method, but it looks better to me.