Skip to content

Low level API

Daniel Frantik edited this page Apr 1, 2018 · 8 revisions

First option is to use low-level API like other mikrotik libraries do. It allows you to send API commands to mikrotik router and read response to these command. If you are not sure about API syntax, see this wiki page and mikrotik documentation. REMARKS: API is not the same as console.

The main entry point is mikrotik router connection object (ITikConnection). Instance of connection object should be instancied via factory (ConnectionFactory).

using tik4net;
// ...
connection = ConnectionFactory.OpenConnection(TikConnectionType.Api, HOST, USER, PASS);
string[] command = new string[] { "/interface/print" };
IEnumerable<ITikSentence> result = Connection.CallCommandSync(command);
// returns list of respose sentences

Response sentence is object with list of response words (like properties of loaded object). See ITikSentence.Words dictionary.

// for example how to load sentences see example above
foreach (var resultItem in result)
  Console.WriteLine(resultItem.Words["name"]);

There are three types of response sentences:

  • ITikReSentence - response sentence from mikrotik router with !re data. It is data sentence (typically when list of entities is requested).
  • ITikDoneSentence - response sentence from mikrotik router with !done status. It is last sentence from sucessfull operation.
  • ITikTrapSentence - response sentence from mikrotik router with !trap status. This sentence is returned when any error occurs.

NOTE: See mikrotik wiki for details about response sentences.

ITikReSentence

Coresponds to !re sentences from mikrotik router (Data replies begin with !re). Main methods to read response fields (object properties):

  • GetResponseField
  • TryGetResponseField
  • GetResponseFieldOrDefault

ITikDoneSentence

Coresponds to !done sentence from mikrotik router. (Last reply for every sentence is reply that has first word !done)

This sentence is just trivial ancestor of ITikSentence base interface.

ITikTrapSentence

Coresponds to !trap sentence from mikrotik router. (When for some reason API sentence fails trap is sent in return accompanied with message attribute and on some occasions category argument.) Details about exception are stored in properties:

  • Message
  • CategoryCode
  • CategoryDescription

More complex example of usage

using tik4net;
// ...
connection = ConnectionFactory.OpenConnection(TikConnectionType.Api, HOST, USER, PASS);

string[] command = new string[] { "/interface/print" };
IEnumerable<ITikSentence> result = Connection.CallCommandSync(command);

foreach(ITikSentence sentence in result)
{
  if (sentence is ITikTrapSentence)
    Console.WriteLine("Some error occurs: {0}", ((ITikTrapSentence)sentence).Message);
  else if (sentence is ITikDoneSentence)
    Console.WriteLine("... DONE ..."); // last sentence
  else if (sentence is ITikReSentence)
  {
    foreach(var wordPair in sentence.Words)
    {
       Console.WriteLine("  {0}={1}", wordPair.Key, wordPair.Value);
    }
  }
  else
    throw new NotImplementedException("Unknown sentence type");
}