Community discussions

MikroTik App
 
ourweb
just joined
Topic Author
Posts: 11
Joined: Thu Feb 15, 2018 4:14 am

API set command

Thu Feb 15, 2018 8:40 pm

I am trying to write a c# windows program to manage users. I am having a hard time using the /tool//user-manager/user/set command to reset thier password. What I am getting back is an invalid command error. All other commands work fine but the set commands. It is hard to program without having a complete API manual. I do not know what I am doing wrong. Can other people use the set command?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API set command

Thu Feb 15, 2018 9:08 pm

Can you show your actual code?

When you say other commands... Do you mean remove/enable/disable? Or just "add"? Because with the exception of "add", other commands require the ID of the item(s) you want to target in the "numbers" argument, and you first need to get with a "print" command.

It's a common problem for newcomers to want to just specify a number as an identifier or write "find" as part of the set/remove/enable/disable command, which is not how you do it.
 
ourweb
just joined
Topic Author
Posts: 11
Joined: Thu Feb 15, 2018 4:14 am

Re: API set command

Thu Feb 15, 2018 10:07 pm

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using mikumlib;

//namespace MKuserman
//{
public class MKuserman
{
private string url = "/tool/user-manager/user/";
// private string url = "/ip/hotspot/user/";

private mkusermanager mk;
private string mkserver;
private string mkusername;
private string mkpassword;
public List<string> output;

public MKuserman(string server, string username, string password)
{
mkserver = server;
mkusername = username;
mkpassword = password;

mk = new mkusermanager(mkserver, 8728, mkusername,mkpassword);
mk.Connect();
mk.Login();
}

public bool Login() { return mk.IsConnected(); }

public void Add(string username,string password, string profile)
{
Vouchers tmp = mk.CreateVoucher(profile, username, password);
}

public void Remove(string userid)
{
if (Login())
{
var del = mk.RemoveVoucher(userid);
}
}

public void Setpassword(Vouchers user, string password)
{
if (Login())
{
var cmd = url + "set=numbers=" + user.Id +
"=password=" + password;
mk.Send(cmd, true);
var output = mk.Read();
}
}

public void Setphone(Vouchers user, string phone)
{
if (Login())
{
var cmd = url + "set=numbers=" + user.Id + "=phone=" + phone;
mk.Send(cmd, true);
var output = mk.Read();
}
}
public List<Vouchers> Getusers()
{




var info = mk.LoadVouchers();

return info;
}
}
//}
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API set command

Thu Feb 15, 2018 10:30 pm

Each call to the Send() method represents one API word.

And each sentence contains multiple words, plus a single empty one. So f.e.
if (Login())
{
mk.Send(url + "set");
mk.Send("=numbers=" + user.Id);
mk.Send("=password=" + password);
mk.Send("");

var output = mk.Read();
}
 
ourweb
just joined
Topic Author
Posts: 11
Joined: Thu Feb 15, 2018 4:14 am

Re: API set command

Fri Feb 16, 2018 3:32 am

No error. but did mot set
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API set command

Fri Feb 16, 2018 12:24 pm

Looking more closely at the source of mikumlib, the ID you get with its methods doesn't include the "*". However, that is required when actually giving the ID to RouterOS.

So make that
mk.Send("=numbers=*" + user.Id);
 
ourweb
just joined
Topic Author
Posts: 11
Joined: Thu Feb 15, 2018 4:14 am

Re: API set command

Fri Feb 16, 2018 5:57 pm

THAT WORK!!!!!!!! Thank you for the help.

Who is online

Users browsing this forum: No registered users and 66 guests