Community discussions

 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1264
Joined: Sun Sep 18, 2011 7:00 pm

trying to remove dhcp lease in C#

Wed Aug 29, 2018 11:57 am

Hello ,
I'm tryin to remove dhcp clients by using this all this commands (every time another command....) :
mikrotik.Send("/ip/dhcp-server/lease/remove");
mikrotik.Send("?=server=dhcp1" , true);
mikrotik.Send("?server=dhcp1" , true);
mikrotik.Send("?server=all" , true);
mikrotik.Send("?=server=all" , true);
foreach ?(string m in mikrotik.Read())
{
}
in all of them I get no error - "!done"
but it doesn't remove nothing ...

but when I do this :
mikrotik.Send("=.id=*2" , true);
it remove me the *2 client...


why?
what is wrong?

Thanks ,
 
nescafe2002
Long time Member
Long time Member
Posts: 622
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: trying to remove dhcp lease in C#

Wed Aug 29, 2018 1:13 pm

You are using a query word in a non-print operation, which is not supported.

https://wiki.mikrotik.com/wiki/Manual:API#Queries
print command accepts query words that limit set of returned sentences. This feature is available since RouterOS 3.21.

Alternative is to query first and then remove by id:

var ids = new List<string>();

mikrotik.Send("/ip/dhcp-server/lease/print");
mikrotik.Send("?-server", true);

foreach (var h in mikrotik.Read())
{
  var startIndex = h.IndexOf(".id=") + 4;
  if (startIndex >= 4)
  {
    var endIndex = h.IndexOf("=", startIndex);
    var id = h.Substring(startIndex, endIndex - startIndex);
    ids.Add(id);
  }
}

foreach (var id in ids)
{
  mikrotik.Send("/ip/dhcp-server/lease/remove");
  mikrotik.Send("=.id=" + id, true);
  Console.WriteLine(mikrotik.Read());
}

Note that while Winbox shows server=all, the server is actually undefined for these entries (query word -server).
 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1264
Joined: Sun Sep 18, 2011 7:00 pm

Re: trying to remove dhcp lease in C#

Wed Aug 29, 2018 1:33 pm

yes ,
this is what I did for now
saved all the Id and then remove them 1 by 1

Thanks ,
 
nescafe2002
Long time Member
Long time Member
Posts: 622
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: trying to remove dhcp lease in C#

Wed Aug 29, 2018 2:33 pm

The Tik4Net (strong-typed entity) example:

https://github.com/danikf/tik4net

using tik4net;
using tik4net.Objects;
using tik4net.Objects.Ip.DhcpServer;

var host = "192.168.88.1";
var user = "admin";
var pass = "password";

using (var conn = tik4net.ConnectionFactory.OpenConnection(TikConnectionType.Api, host, user, pass))
{
  var filter = conn.CreateParameter("-server", "");
  var leases = conn.LoadList<DhcpServerLease>(filter);

  foreach (var lease in leases)
  {
    conn.Delete(lease);
  }
}
 
David1234
Forum Guru
Forum Guru
Topic Author
Posts: 1264
Joined: Sun Sep 18, 2011 7:00 pm

Re: trying to remove dhcp lease in C#

Thu Aug 30, 2018 10:09 am

all my code is in the mikrotik API and not the Tik4Net
I wnat to start working with this - when I have time
:-)

Thanks ,
 
nescafe2002
Long time Member
Long time Member
Posts: 622
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: trying to remove dhcp lease in C#

Fri Aug 31, 2018 4:35 pm

You have wasted a lot of time already. Just pointing out there are better and quicker solutions.

Assuming you have never installed a NuGet package?

Who is online

Users browsing this forum: No registered users and 4 guests