Skip to content

From the console to tik4net

Daniel Frantík edited this page Nov 27, 2026 · 4 revisions

From the console to tik4net

You know the RouterOS console. This page maps what you type there to the two calls you would write instead: the O/R mapper (typed entities) and the ADO.NET-like API (commands and parameters). Both run on every transport.

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

var setup = new TikConnectionSetup("192.168.88.1", "admin", "");
using (ITikConnection connection = setup.Create(TikConnectionType.Api))
{
    // /ip address print where interface=ether1
    foreach (var address in connection.LoadList<IpAddress>(connection.CreateParameter("interface", "ether1")))
        Console.WriteLine(address.Address);
}

The mapping

The path is the API spelling (/ip/address, slashes, no spaces); the menu's entity class is on Entity reference.

You type O/R mapper Command
/ip address print connection.LoadAll<IpAddress>() connection.CreateCommand("/ip/address/print").ExecuteList()
/ip address print where interface=ether1 connection.LoadList<IpAddress>(connection.CreateParameter("interface", "ether1")) connection.CreateCommandAndParameters("/ip/address/print", "interface", "ether1").ExecuteList()
/ip address print detail LoadAll — an entity that needs detail asks for it itself CreateParameter("detail", "", TikCommandParameterFormat.NameValue)
/interface print where name=ether1 (one row) connection.LoadByName<Interface>("ether1"), or LoadSingle<T>(filter) ….ExecuteSingleRow()
/system identity print (a singleton) connection.LoadSingle<SystemIdentity>() connection.CreateCommand("/system/identity/print").ExecuteSingleRow()
:put [/system identity get name] connection.LoadSingle<SystemIdentity>().Name connection.CreateCommand("/system/identity/print").ExecuteScalar("name")
/ip address add address=10.0.0.1/24 interface=ether1 connection.Save(new IpAddress { Address = "10.0.0.1/24", Interface = "ether1" }) — fills Id connection.CreateCommandAndParameters("/ip/address/add", "address", "10.0.0.1/24", "interface", "ether1").ExecuteScalar() — returns the .id
/ip address set [find address="10.0.0.1/24"] comment=lan load it, address.Comment = "lan", connection.Save(address) — sends only comment CreateCommandAndParameters("/ip/address/set", ".id", id, "comment", "lan").ExecuteNonQuery()
/ip address unset [find …] comment address.Comment = null, connection.Save(address) CreateCommandAndParameters("/ip/address/unset", ".id", id, "value-name", "comment").ExecuteNonQuery()
/ip address remove [find …] connection.Delete(address), or connection.DeleteById<IpAddress>(id) with only the .id CreateCommandAndParameters("/ip/address/remove", ".id", id).ExecuteNonQuery()
/ip address disable [find …] / enable address.Disabled = true (or false), connection.Save(address) CreateCommandAndParameters("/ip/address/disable", ".id", id).ExecuteNonQuery()
/ip firewall filter move 3 destination=1 connection.Move(rule, ruleBefore), connection.MoveToEnd(rule) CreateCommandAndParameters("/ip/firewall/filter/move", "numbers", id, "destination", beforeId).ExecuteNonQuery()
/ip firewall address-list remove [find list=blocked] LoadList with the filter, then Delete each print with the filter, then remove each .id
/interface monitor-traffic ether1 once connection.GetInterfaceMonitorTrafficSnapshot("ether1") —
/ping 10.0.0.1 count=4 connection.Ping("10.0.0.1", 4) —
/tool torch ether1 (runs until Ctrl+C) LoadWithCallbackAsync<ToolTorch>(…, token), stopped by cancelling the token ExecuteWithCallbackAsync(…, token), stopped by cancelling the token
/ip address listen LoadListenWithCallbackAsync<IpAddress>(…) /ip/address/listen with ExecuteWithCallbackAsync
/export, :foreach …, any script — the line itself, on a CLI transport: cli.CreateRawCommand("/export").ExecuteScalar()
  • [find …] is a load. The console's find selects rows by any condition; tik4net reads them with a filter and then writes each one by its .id. A filter is an exact match on one field per parameter, several parameters meaning and — the full query language is on Low-level API.
  • A value in the console's spelling (1d, !22,8291, syn,!ack) goes into any typed property through Tik.Wire: server.LeaseTime = Tik.Wire("1d"), rule.DstPort = Tik.Wire("!22,8291"). It is parsed for that property and refused if the type cannot hold it — see TikField.
  • A row number (set 3) is the console's own; it does not exist outside one print. tik4net always addresses a row by its .id.
  • Scripting and free text need the console's own language: CLI raw commands.

One task, written both ways

using tik4net;
using tik4net.Objects;
using tik4net.Objects.Ip.Firewall;

// /ip firewall address-list add list=blocked address=10.20.30.40 comment="tik4net"
var entry = new FirewallAddressList { List = "blocked", Address = "10.20.30.40", Comment = "tik4net" };
connection.Save(entry);

// /ip firewall address-list set [find list=blocked address=10.20.30.40] timeout=1d
entry.Timeout = Tik.Wire("1d");
connection.Save(entry);

// /ip firewall address-list remove [find list=blocked address=10.20.30.40]
connection.Delete(entry);

// the same three at the command level
string id = connection.CreateCommandAndParameters("/ip/firewall/address-list/add",
    "list", "blocked", "address", "10.20.30.40", "comment", "tik4net").ExecuteScalar();
connection.CreateCommandAndParameters("/ip/firewall/address-list/set", ".id", id, "timeout", "1d").ExecuteNonQuery();
connection.CreateCommandAndParameters("/ip/firewall/address-list/remove", ".id", id).ExecuteNonQuery();

See also

Clone this wiki locally