Community discussions

MikroTik App
 
abrpr
just joined
Topic Author
Posts: 7
Joined: Sat May 13, 2017 12:18 pm
Location: Iran
Contact:

add user C# API

Sat May 13, 2017 7:53 pm

hi guys . .
i wanna add user in usermanager by API . .
i run this code :
static void Main()
    {
        MK mikrotik = new MK();
        if (mikrotik.Login("admin","password"))
        {
            mikrotik.Send("/tool/user-manager/user/add", false);
            mikrotik.Send("=customer=admin=", false);
            mikrotik.Send("=name=test=", false);
            mikrotik.Send("=password=testtest=", false);
            foreach (string h in mikrotik.Read())
            {
                Console.WriteLine(h);
            }
        }
but i face this error :
trap=message=no such command prefix !done
help me plz ..
what is wrong ?
 
User avatar
ahmedalmi
Frequent Visitor
Frequent Visitor
Posts: 75
Joined: Sat Sep 13, 2014 5:52 pm
Location: sana'a yemen
Contact:

Re: add user C# API

Sat May 13, 2017 11:04 pm

mikrotik.Send("=password=testtest=", true);

last send must be "ture"
 
abrpr
just joined
Topic Author
Posts: 7
Joined: Sat May 13, 2017 12:18 pm
Location: Iran
Contact:

Re: add user C# API

Sat May 13, 2017 11:26 pm

mikrotik.Send("=password=testtest=", true);

last send must be "ture"
thanks for your help pal . .
but the problem not solved .. :(
again i face this error :
!trap=message=no such command prefix !done
Last edited by abrpr on Sat May 13, 2017 11:41 pm, edited 1 time in total.
 
User avatar
ahmedalmi
Frequent Visitor
Frequent Visitor
Posts: 75
Joined: Sat Sep 13, 2014 5:52 pm
Location: sana'a yemen
Contact:

Re: add user C# API

Sat May 13, 2017 11:37 pm

ok did you set ip ?

try this


MK mikrotik = new MK("your ip here");
if (mikrotik.Login("admin","password"))
{
mikrotik.Send("/tool/user-manager/user/add");
mikrotik.Send("=customer=admin");
mikrotik.Send("=username=test");
mikrotik.Send("=password=testtest", true);
string result = mikrotik.Read();
}

or send me your clas and i will edit it
 
User avatar
ahmedalmi
Frequent Visitor
Frequent Visitor
Posts: 75
Joined: Sat Sep 13, 2014 5:52 pm
Location: sana'a yemen
Contact:

Re: add user C# API

Sat May 13, 2017 11:39 pm

or if your mikrotik virson up 6.18 you must change " name " to " username"

mikrotik.Send("=name=test=", false);
to
mikrotik.Send("=username=test", false);
 
abrpr
just joined
Topic Author
Posts: 7
Joined: Sat May 13, 2017 12:18 pm
Location: Iran
Contact:

Re: add user C# API

Sun May 14, 2017 12:25 am

ok did you set ip ?

try this


MK mikrotik = new MK("your ip here");
if (mikrotik.Login("admin","password"))
{
mikrotik.Send("/tool/user-manager/user/add");
mikrotik.Send("=customer=admin");
mikrotik.Send("=username=test");
mikrotik.Send("=password=testtest", true);
string result = mikrotik.Read();
}

or send me your clas and i will edit it
thank you so much bro :)
it works . .
:) :) :)
 
User avatar
ahmedalmi
Frequent Visitor
Frequent Visitor
Posts: 75
Joined: Sat Sep 13, 2014 5:52 pm
Location: sana'a yemen
Contact:

Re: add user C# API

Sun May 14, 2017 12:54 am

Focus on changes in virsons
like name, username
and in actvate profile you must change user to numbers
and if you want to use username in actvate you must use like this code
in new virson like 6.33 and up

Code: Select all

this.mikrotik.Send("/tool/user-manager/user/create-and-activate-profile");
this.mikrotik.Send("=customer=admin");
this.mikrotik.Send("=numbers=test");
this.mikrotik.Send("=profile=profilename", true);
and in virson like 5.25

Code: Select all

this.mikrotik.Send("/tool/user-manager/user/create-and-activate-profile");
this.mikrotik.Send("=customer=admin");
this.mikrotik.Send("=user=test");
this.mikrotik.Send("=profile=profilename", true);
 
abrpr
just joined
Topic Author
Posts: 7
Joined: Sat May 13, 2017 12:18 pm
Location: Iran
Contact:

Re: add user C# API

Thu May 18, 2017 1:30 am

hi pal . .
i have another problem //
i use this code to find specific user :
            Console.Write("\n\n\t\t Write Your Username : ");
            string name = Console.ReadLine();
            mikrotik.Send("/tool/user-manager/user/print");
            mikrotik.Send("?username="+name, true);
            foreach (string h in mikrotik.Read())
            {
                Console.WriteLine( h);
            }
it works . .
but i want to display some part of info . .
for ex i want to display just username not all the information of that user . .
what should i do ?
how should i filter "h" to display specific info ?
 
User avatar
ahmedalmi
Frequent Visitor
Frequent Visitor
Posts: 75
Joined: Sat Sep 13, 2014 5:52 pm
Location: sana'a yemen
Contact:

Re: add user C# API

Thu May 18, 2017 2:14 am

hi pal . .
i have another problem //
i use this code to find specific user :
            Console.Write("\n\n\t\t Write Your Username : ");
            string name = Console.ReadLine();
            mikrotik.Send("/tool/user-manager/user/print");
            mikrotik.Send("?username="+name, true);
            foreach (string h in mikrotik.Read())
            {
                Console.WriteLine( h);
            }
it works . .
but i want to display some part of info . .
for ex i want to display just username not all the information of that user . .
what should i do ?
how should i filter "h" to display specific info ?
you must define what info you want
like this

Code: Select all

mikrotik.Send("/tool/user-manager/user/print");
mikrotik.Send("?username="+name);
mikrotik.Send("=.proplist=.id,username,password,actual-profile", true);
 
abrpr
just joined
Topic Author
Posts: 7
Joined: Sat May 13, 2017 12:18 pm
Location: Iran
Contact:

Re: add user C# API

Fri May 19, 2017 8:58 pm

hey bro , you are my hero :)))))
i have another question . .
    Stream connection;
    TcpClient con;

    public MK(string ip)
    {
        con = new TcpClient();
        con.Connect(ip, 8728);
        connection = (Stream)con.GetStream();
    }
with above code :
when i enter wrong ip my program goes crashing . .
i wanna when i enter wrong ip it show message that ip is wrong . .
what should i do with this code ?
 
User avatar
ahmedalmi
Frequent Visitor
Frequent Visitor
Posts: 75
Joined: Sat Sep 13, 2014 5:52 pm
Location: sana'a yemen
Contact:

Re: add user C# API

Sat May 20, 2017 7:08 am

hey bro , you are my hero :)))))
i have another question . .
    Stream connection;
    TcpClient con;

    public MK(string ip)
    {
        con = new TcpClient();
        con.Connect(ip, 8728);
        connection = (Stream)con.GetStream();
    }
with above code :
when i enter wrong ip my program goes crashing . .
i wanna when i enter wrong ip it show message that ip is wrong . .
what should i do with this code ?

i think the message for all wrong is one
for wrong ip , or port, or username and password ( one message for wrong in connection )
so you just use one message for any problem in connect.
i think it is no important for this
 
Itamar
just joined
Posts: 2
Joined: Wed May 23, 2018 6:56 am

Re: add user C# API

Sat May 26, 2018 3:38 am

Hello bro..., I have a problem when registering the user, the problem is that I run the code twice, it only registers the user's name and the password does not register what I can do to correct that error

MK mikrotik = new MK("10.10.0.254");
if (!mikrotik.Login("itamar", "12345678"))
{
Console.WriteLine("No se pudo entrar a la cuenta");
Console.ReadKey();
mikrotik.Close();
return;
}
mikrotik.Send("/ip/hotspot/user/remove",false);
mikrotik.Send("=name=n", false);
mikrotik.Send("=password=n", true);
mikrotik.Send("=profile=profile1=", true);


Console.WriteLine("usuario creado");

foreach (string h in mikrotik.Read())
{
Console.WriteLine(h);
}
Console.ReadKey();
 
Itamar
just joined
Posts: 2
Joined: Wed May 23, 2018 6:56 am

Re: add user C# API

Sat May 26, 2018 3:40 am

mikrotik.Send("=password=testtest=", true);

last send must be "ture"
Hello sister, I have a problem when registering the user, the problem is that I run the code twice, it only registers the user's name and the password does not register what I can do to correct that error

MK mikrotik = new MK("10.10.0.254");
if (!mikrotik.Login("itamar", "12345678"))
{
Console.WriteLine("No se pudo entrar a la cuenta");
Console.ReadKey();
mikrotik.Close();
return;
}
mikrotik.Send("/ip/hotspot/user/remove",false);
mikrotik.Send("=name=n", false);
mikrotik.Send("=password=n", true);
mikrotik.Send("=profile=profile1=", true);


Console.WriteLine("usuario creado");

foreach (string h in mikrotik.Read())
{
Console.WriteLine(h);
}
Console.ReadKey();
 
nescafe2002
Forum Veteran
Forum Veteran
Posts: 897
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: add user C# API

Sat May 26, 2018 4:26 pm

Please note that there is a fault in the send method of the MT C# API example:
public void Send(string co, bool endsentence)
{
  byte[] bajty = Encoding.ASCII.GetBytes(co.ToCharArray());
  byte[] velikost = EncodeLength(bajty.Length);
  connection.Write(velikost, 0, velikost.Length);
  connection.Write(bajty, 0, bajty.Length);
  connection.WriteByte(0);
}

The problem is: the Send(string, bool) overload disregards the endsentence parameter, always sending a null byte terminator.

This should work (changing /remove into /add as it seems you're trying to add a user):
mikrotik.Send("/ip/hotspot/user/add");
mikrotik.Send("=name=n");
mikrotik.Send("=password=n");
mikrotik.Send("=profile=profile1", true);

Edit: Wiki has been fixed

Who is online

Users browsing this forum: Google [Bot] and 30 guests