Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Printing, adding, editing and removing items and data

Aluisio Amaral edited this page Oct 31, 2017 · 8 revisions

The CRUD

When printing, adding, editing or removing data, you will use the write() function:

RouterOSAPI#write(parameters [, ...moreParameters]): Promise

  • parameters <string|Array> - the data you want to send to the router.
  • moreParameters <string|Array> - convenient way to add more parameters to the data to send.

Printing data

You can print data doing the following:

conn.connect().then(() => {
    // Connection successful

    // Printing the interface menu
    conn.write("/interface/print").then((data) => {
        // Got the interfaces
    }).catch((err) => {
        // Got error trying to print the interfaces
    });

}).catch((err) => {
    // Got an error while trying to connect
});

Filtering and quering

The write() function accepts multiple parameters, you can do it like:

// This will print where the query returns true to all parameters
conn.write("/ip/address/print", "?address=192.168.88.1", "?disabled=no").then((data) => {
    // Got the addresses
}).catch((err) => {
    // Got error trying to print the addresses
});


// You can do the same by passing an array like this
conn.write([
    "/ip/address/print",
    "?address=192.168.88.1",
    "?disabled=no"
]).then((data) => {
    // Got the addresses
}).catch((err) => {
    // Got error trying to print the addresses
});


// Or mix them
conn.write("/ip/address/print", [
    "?address=192.168.88.1",
    "?disabled=no"
]).then((data) => {
    // Got the addresses
}).catch((err) => {
    // Got error trying to print the addresses
});

WARNING: You should not use a listen command here, nor a print command with an interval or any other command which keeps pulling data (/tool/torch for example), for that, go to the streaming section.

Note that, like the mikrotik wiki states, you can use either print or getall, they do the same thing. You can find other ways of quering on the mikrotik wiki here.

Adding

When adding, the promise should return an array with a ret property, which is the .id of the just added item:

conn.write([
    "/ip/address/add",
    "=interface=ether2",
    "=address=192.168.88.15"
]).then((data) => {
    // Added the address
    console.log(data[0].ret); // Should be something like *A6
}).catch((err) => {
    // Got error trying to add the address
    // If you get an error it will throw an Error object, with a message property
    console.log(err.message);
});

Editing

Editing is a little more complex than adding items, since you can edit a firewall rule, or change a configuration over the SNMP. Some menus of the RouterOS requires you to unset values to empty their data if you so desire, so keep that in mind when editing. Here's an example:

// Editing an item from a list of items
conn.write([
    "/ip/address/set",
    "=address=192.168.88.20",
    "=.id=*A6" // or, if you know the number: "=numbers=2"
]).then((data) => {
    // Edited the address
}).catch((err) => {
    // Got error trying to edit the address
    // If you get an error it will throw an Error object, with a message property
    console.log(err.message);
});

// Editing config from a menu
conn.write([
    "/snmp/set",
    "=enabled=yes"
]).then((data) => {
    // Enabled snmp
}).catch((err) => {
    // Got error trying to edit the snmp
    console.log(err.message);
});

Unset

To unset a value:

// Editing an item from a list of items
conn.write([
    "/ip/firewall/filter/unset",
    "=value-name=out-interface",
    "=.id=*D0" // or, if you know the number: "=numbers=6"
]).then((data) => {
    // Unset the out-interface
}).catch((err) => {
    // Got error trying to unset the out-interface
    // If you get an error it will throw an Error object, with a message property
    console.log(err.message);
});

Removing

Removing goes the same way as editing:

// Editing an item from a list of items
conn.write([
    "/ip/address/remove",
    "=.id=*A6" // or, if you know the number: "=numbers=5"
]).then((data) => {
    // Removed the address
}).catch((err) => {
    // Got error trying to remove the address
    // If you get an error it will throw an Error object, with a message property
    console.log(err.message);
});

Next: Streaming data, pausing, resuming and stopping