-
Notifications
You must be signed in to change notification settings - Fork 94
Home
Daniel Frantik edited this page Apr 9, 2017
·
21 revisions
- Easy to use (a few lines of code)
- R/W access
- Support for paralel commands (like torch)
- ITikConnection as unified entry point to router
- Low-level API as other simple mikrotik API libraries
- ADO.NET like with strong-typed API (connection, commands, parameters)
-
Highlevel O/R mapper like API
- Strong-typed objects for Mikrotik entities (e.q. QueueTree, FirewallMangle, ...)
- Support for ordering lists of entities on router
- Support for merging collections of mikrotik objects with state on mikrotik router (you could simply prepare expected state and only necessary operations are performed during merge)
- Clean design, clear code (of course, it could be always better ...)
Documnetation:
- For details about tik4net usage see how to use wiki page.
- For CRUD examples see CRUD examples for all APIs wiki page.
- For VisualBasic trivial example see VB example
#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();
}