Mikrotik.Net 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Mikrotik.Net --version 1.0.3
NuGet\Install-Package Mikrotik.Net -Version 1.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mikrotik.Net" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Mikrotik.Net --version 1.0.3
#r "nuget: Mikrotik.Net, 1.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Mikrotik.Net as a Cake Addin
#addin nuget:?package=Mikrotik.Net&version=1.0.3

// Install Mikrotik.Net as a Cake Tool
#tool nuget:?package=Mikrotik.Net&version=1.0.3

MikrotikDotNet

MikrotikDotNet is a lightwaight and easy to use ADO.NET like library for Mikrotik Api with extensibility and performance in mind.

NuGet package:

Install-Package Mikrotik.Net

How to use:

Simple command with parameters (ExecuteNonQuery):

using (var conn = new MKConnection(IPADDRESS, USERNAME, PASSWORD))
{
    conn.Open();
    var cmd = conn.CreateCommand("interface pppoe-client add");
    cmd.Parameters.Add("interface", "ether1");
    cmd.Parameters.Add("Name", "Test"); // You can use PascalCase or kebab-case in parameter name.
    cmd.Parameters.Add("user","Test");
    cmd.Parameters.Add("password", "Test");
    cmd.ExecuteNonQuery();
}

Read response: (ExecuteReader):

using (var conn = new MKConnection(IPADDRESS, USERNAME, PASSWORD))
{
  conn.Open();
  var cmd = conn.CreateCommand("ip address print");
  var result = cmd.ExecuteReader();
  foreach (var line in result)
    Console.WriteLine(line);
  
}

Result (Raw api response):

!re=.id=*4=address=10.20.1.19/16=network=10.20.0.0=interface=bridge1=actual-interface=bridge1=invalid=false=dynamic=false=disabled=false
!re=.id=*5=address=172.16.0.1/30=network=172.16.0.0=interface=bridge1=actual-interface=bridge1=invalid=false=dynamic=false=disabled=false
!re=.id=*6=address=172.19.1.19/19=network=172.19.0.0=interface=bridge1=actual-interface=bridge1=invalid=false=dynamic=false=disabled=false

To deserialize response: (ExecuteReader<T>)

class MyIpAddress
{
    public string MKID { get; set; }  //MKID alwase referce to .id field in response.
    public string Address { get; set; } // Use PascalCase naming style for properties. it will convert from/to kebab-case naming.
    public string Interface { get; set; }
}
//------------------------------------------------
using (var conn = new MKConnection(IPADDRESS, USERNAME, PASSWORD))
{
    conn.Open();
    var cmd = conn.CreateCommand("ip address print");
    var result = cmd.ExecuteReader<MyIpAddress>();

    foreach (var ip in result)
        Console.WriteLine($"{ip.MKID} - {ip.Address} - {ip.Interface}");

}

Result:

*4 - 10.20.1.19/16 - bridge1
*5 - 172.16.0.1/30 - bridge1
*6 - 172.19.1.19/19 - bridge1

Note: Using method ExecuteReader<T> only reads the fields that are present is the given type, using the .proplist field in query.

To get dynamic object response: (ExecuteReaderDynamic) you can get response object without defining any model class.

class MyIpAddress
{
    public string MKID { get; set; }  //MKID alwase referce to .id field in response.
    public string Address { get; set; } // Use PascalCase naming style for properties. it will convert from/to kebab-case naming.
    public string Interface { get; set; }
}
//------------------------------------------------
using (var conn = new MKConnection(IPADDRESS, USERNAME, PASSWORD))
{
    conn.Open();
    var cmd = conn.CreateCommand("ip address print");
    var result = cmd.ExecuteReaderDynamic();

    foreach (var ip in result)
        Console.WriteLine($"{ip.Id} - {ip.Address} - {ip.Interface}"); //MKID switched to Id
}

Result:

*4 - 10.20.1.19/16 - bridge1
*5 - 172.16.0.1/30 - bridge1
*6 - 172.19.1.19/19 - bridge1

Note: Using method ExecuteReaderDynamic reads all fields from the router it will increase response payload.

Read data from background commands: (ExecuteBackground) Some commands works in background ( like ping,bandwith test, discovert,...)

using (var conn = new MKConnection(IPADDRESS, USERNAME, PASSWORD))
{
    conn.Open();
    var cmd = conn.CreateCommand("ping");
    cmd.Parameters.Add("address", "10.20.0.4");
    cmd.Parameters.Add("count", "3");
    cmd.Parameters.Add("interval", "1");
    cmd.ExecuteBackground();

    Thread.Sleep(5000); 
    
    var result = cmd.ExecuteReaderDynamic();
    foreach (var ip in result)
        Console.WriteLine($"{ip.Host} - {ip.Time} - {ip.Ttl}");

}

Result:

10.20.0.4 - 13ms - 128
10.20.0.4 - 6ms - 128
10.20.0.4 - 4ms - 128
Product Compatible and additional computed target framework versions.
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.0 1,465 10/2/2022
1.2.0 938 5/7/2022
1.0.3 2,081 10/22/2016

Bug fix for reading strong type reading. ExecuteReader<T>()