C# API - tik4net on GitHub

I have just published my API client for .NET. It implements execution of parallel queries which are easily handled due to ADO.NET-like interface + O/R mapper like extensions to provide strong-typed experience (it supports both single entity and list of entities).

Please leave message in this thread if you need some feature to be implemented or if you have any relevant question.

Repository: https://github.com/danikf/tik4net
Wiki: https://github.com/danikf/tik4net/wiki
How to use: https://github.com/danikf/tik4net/wiki/How-to-use-tik4net-library
nuget package: https://www.nuget.org/packages/tik4net/
Releases (usage of nuget package is recommended):

tik4net-3.5.0.zip (1.72 MB)
tik4net-3.4.0.zip (1.44 MB)
Features:

  • ITikConnection lowlevel API (send command / read response, async commands)
  • ADO.NET like api (ITikCommand + various Execute… methods)
  • O/R mapper to/from entity classes. (connection.LoadList())
  • Release also contains C# entity code generators to support semi-automatic generation of custom entities from running mikrotik router and from mikrotik wiki site (from oficial documentation)
  • API-SSL > support
  • New mikrotik (from v. 6.43) login process support
  • Dlls builded for .NET 3.5, 4.0, 4.5.x, 4.6.x, netcoreapp1.1, netcoreapp2.0, netstandart1.3, netstandard1.4, netstandard1.6
  • Functional with xamarin and other .NET runtimes based on Mono

Reference downloaded dlls only if you are not able to use > nuget package > or > GitHub sources.

Examples
For read/write examples see API comparison CRUD examples wiki page.

Read and print mikrotik router identity

using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api))
{
  connection.Open(HOST, USER, PASS);
  ITikCommand cmd = connection.CreateCommand("/system/identity/print");
  Console.WriteLine(cmd.ExecuteScalar());
}

Example of async Torch command

using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api))
{
  connection.Open(HOST, USER, PASS);
  var loadingContext = connection.LoadAsync<ToolTorch>(
    torchItem => Console.WriteLine(torchItem.ToString()),
    error => Console.WriteLine(error.ToString()),                                                
    connection.CreateParameter("interface", interfaceName),   
    connection.CreateParameter("port", "any"),
    connection.CreateParameter("src-address", "0.0.0.0/0"),
    connection.CreateParameter("dst-address", "0.0.0.0/0"));

  Console.ReadLine();
  loadingContext.Cancel();
}

read all log entries

var logs = connection.LoadList<Log>();
foreach (Log log in logs)
{
    Console.WriteLine("{0}[{1}]: {2}", log.Time, log.Topics, log.Message);
}

firewall management

//find first firewall rule
var firstFirewallRule = connection.LoadAll<FirewallFilter>().First();

// create new firewall rule as first rule in list
var firewallFilter = new FirewallFilter()
{
   Chain = FirewallFilter.ChainType.Forward,
   Action = FirewallFilter.ActionType.Accept,
};
connection.Save(firewallFilter);
connection.Move(firewallFilter, firstFirewallRule);

_NOTE: please do not use deprecated 0.9.7 incompatible version of tik4net from https://code.google.com/p/mikrotik4net/._
tik4net-3.3.0.zip (1.42 MB)

Chupaka - thank you for inspiration how to handle paralel queries.

D
BTW: both your and mine code are not realy thread-safe :wink:

Released new version with updates. Added versions for .NET 3.5, .NET 4.0 and .NET 4.5.2

Released new version 1.2.0.0 with enum support (as field values) and with C# entity code generators.

Enjoy,
D

Example of highlevel API usage (dev branch on GitHub):

// renew IP on dhcp-client interface
connection.LoadAll<IpDhcpClient>().First().Release(connection);

This looks like a great project, and one that would be very useful. Thanks for sharing it with us.

I have just downloaded it and spent some time going through the samples and classes, but I did not notice anything to do with Hotspot, in particular creating and deleting user accounts. Is this functionality available in the library? If not, is it something you will be adding anytime soon?

Again, many thanks.

Mark

Hello,

I am going to import hotspot objects very soon (one week?), but I need betatesters, becase I am not using this feature. If you wold participate in betatesting, your help will be appreciated.

Or you can create your own classes (see TikEntity and TikProperty attributes) and use it with O/R mapper like extensions.

Or you can use ADO like api - handle hotspot management via standard “Execute” command interface like other libraries :slight_smile:

D

Released new version 1.2.2.0 with hotspot user entities (beta).

using tik4net.Objects;
using tik4net.Objects.Ip.Hotspot;



var user = new HotspotUser()
{
    Name = "TEST",
    LimitUptime = "1:00:00",
    Password = "secretpass"
};
_connection.Save(user);

Enjoy,
D

We have a test facility in our office here, and use the Hotspot functionality quite extensively, so would be happy to help test it for you.

Published version 1.3.0.0.

New highlevel entities:

  • Hotspot users
  • Interface (eth/wlan)
var list = Connection.LoadAll<InterfaceWireless.WirelessRegistrationTable>();

Enjoy,
D

Version 1.4.0.0 released:

  • Fixed word length calculation (credits: h44z)
  • Hotspot user management fixed
  • Async API refactoring (cleaning)

Enjoy,
D

Impressive work :smiley: I made use of the older deprecated version but this is awesome hands down! Now Testing!

good evening…
Must capture all connections from an IP address /ip/firewall/connections/print where src-address ~192.168.2.2 . How could perform it? thank you

good evening…
I can not run /IP/firewall/connection/print where src-address ~ “192.168.2.2”.
I need to capture the dst -address.

Obrigado .

Hi,

there are many ways how to handle this task:

using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api))
{
  connection.Open(HOST, USER, PASS);
  1. Via direct API call (low-level access):
  string[] command = new string[]
  {
    "/ip/firewall/connection/print",
    "?src-address=192.168.3.103"
  };
  var result = connection.CallCommandSync(command);
  1. Via ADO.NET like API:
  var command = connection.CreateCommandAndParameters("/ip/firewall/connection/print",
    "src-address", "192.168.3.103");
  var result = command.ExecuteList();
  1. Via highlevel O/R mapper like API:
  // This class will be part of the next release, but you can just put it in your code
    [TikEntity("ip/firewall/connection")]
    public class FirewallConnection
    {
        [TikProperty(".id", IsReadOnly = true, IsMandatory = true)]
        public string Id { get; private set; }

        [TikProperty("connection-mark", IsReadOnly = true)]
        public string ConnectionMark { get; private set; }

        [TikProperty("connection-type", IsReadOnly = true)]
        public string ConnectionType { get; private set; }

        [TikProperty("dst-address", IsReadOnly = true)]
        public string DstAddress { get; private set; }

        [TikProperty("protocol", IsReadOnly = true)]
        public string Protocol { get; private set; }

        [TikProperty("src-address", IsReadOnly = true)]
        public string SrcAddress { get; private set; }

        [TikProperty("tcp-state", IsReadOnly = true)]
        public string TcpState { get; private set; }

        [TikProperty("timeout", IsReadOnly = true)]
        public string Timeout { get; private set; }
    }

  // And the code:
  using tik4net.Objects;
  ...
  var result = connection. LoadList<FirewallConnection>(
    connection.CreateParameter("src-address", "192.168.3.103"));
  1. You can skip filtering part, select all connection items and filter them via C# code (LINQ?). Slow, but simple…

Enjoy,
D

good Morning…
I performed a test as follows

using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api))
{
connection.Open(HOST, USER, PASS);
var command = connection.CreateCommandAndParameters(“/ip/firewall/connection/print”,“src-address”,“192.168.3.69”);
var result = command.ExecuteList();

foreach (var c in result)
{
listBox1.Items.Add(c);
}
}

however I have no result

Probably there is no active connection from ip 192.168.3.69 to router (or connection tracking is switched off in mikrotik configuration).

Try to load all connections without filter:

var command = connection.CreateCommandAndParameters("/ip/firewall/connection/print"); 
var result = command.ExecuteList();

Enjoy,
D

Released new version 1.5.0.0.

Whats new:

Enjoy,
D

Hi danikf
Good work man, Thanks for sharing it.
I am using your dll in vb.net project, can you explain how to set ?#operations by CreateCommandAndParameters to applies operations to the values in the stack.
Example to execute this query.

/interface/print
?type=ether
?type=vlan
?#|

Hi,
I have just updated github sources (update will be part of the next release). With updated version you can simply format command text with filter.

Untyped version:

            var cmd = Connection.CreateCommandAndParameters(@"/interface/print
                            ?type=ether
                            ?type=wlan
                            ?#|");
            var list = cmd.ExecuteList();

Strong-typed version

            var cmd = Connection.CreateCommandAndParameters(@"/interface/print
                            ?type=ether
                            ?type=wlan
                            ?#|");
            var list = cmd.LoadList<Interface>();

The main reasson why parameters stack is not supported is that it will bring high complexity into API (expression trees?). So, I will decide (for this time) not to support this construction via parameters (may be in the future).

Enjoy,
D