-
Notifications
You must be signed in to change notification settings - Fork 100
Low level API
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 the MikroTik router connection object (ITikConnection). An instance of the connection object should be instantiated via a 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 response sentencesResponse 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.
Corresponds to !re sentences from the MikroTik router (data replies begin with !re). Main methods to read response fields (object properties):
- GetResponseField
- TryGetResponseField
- GetResponseFieldOrDefault
Corresponds to the !done sentence from the MikroTik router. (The last reply for every sentence is the reply that has first word !done.)
This sentence is just trivial ancestor of ITikSentence base interface.
Corresponds to the !trap sentence from the MikroTik router. (When an API sentence fails for any reason, a trap is sent in return accompanied by a message attribute and on some occasions a category argument.) Details about the exception are stored in properties:
- Message
- CategoryCode
- CategoryDescription
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("An error occurred: {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");
}