Skip to content
Daniel Frantik edited this page Apr 9, 2017 · 21 revisions

Features

Documnetation:

#Examples: For mikrotik object manipulation examples (R/W) see CRUD examples for all APIs 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());
}
// Creates address-list item
using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api))
{
  connection.Open(HOST, USER, PASS);
  ITikCommand cmd = connection.CreateCommandAndParameters("/ip/firewall/address-list/add",
    "list", "MY_LIST",
    "address", "192.168.1.1");
  var id = cmd.ExecuteScalar();
  Console.WriteLine("Created item with id {0}", id);
}
// Creates address-list item via highlevel API
using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api))
{
  connection.Open(HOST, USER, PASS);
  var newAddressList = new FirewallAddressList()
                {
                    Address = ipAddress,
                    List = listName,
                };
  connection.Save(newAddressList);
  Console.WriteLine("Created item with id {0}", newAddressList.Id);
}
// Delete all address-list items with specific name via highlevel API (SaveListDifferences)
using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api))
{
  var existingAddressList = connection.LoadList<FirewallAddressList>(
    connection.CreateParameter("list", listName)).ToList();
    
  var listClonedBackup = existingAddressList.CloneEntityList(); //creates clone of all entities in list
  existingAddressList.Clear();

  //save differences into mikrotik  (existingAddressList=modified, listClonedBackup=unmodified)
  connection.SaveListDifferences(existingAddressList, listClonedBackup);
}
// 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();
}
You can’t perform that action at this time.