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

Connection

Aluisio Amaral edited this page Mar 6, 2018 · 8 revisions

Summary

Getting started

Before we connect, we must include and create the RouterOSClient object like so:

const RouterOSClient = require('routeros-client').RouterOSClient;

const routeros = new RouterOSClient({
    host: "someplace.com",
    user: "admin",
    password: "somepassword"
});

Here are all the connection options:

interface IRosOptions {
    // Hostname or IP address
    host: string; 

    // Username
    user?: string;

    // Password
    password?: string;
    
    // The API port, not that if you enable TLS, 
    // you must change this port accordingly
    port?: number; 
    
    // Seconds to wait for a connection
    timeout?: number; 
    
    // Either true or TLS options described 
    // here: https://nodejs.org/api/tls.html#tls_new_tls_tlssocket_socket_options
    tls?: TlsOptions; 
    
    // Keep the connection open after connected
    keepalive?: boolean;
}

Connecting

After we have the object, we can then connect by:

connect(): Promise<RosApiMenu>

routeros.connect().then((client) => {
    // Connected successfully

    // The promise returns a client
    // that we can use to execute
    // our tasks, more on that later
    console.log(client);

}).catch((err) => {
    // Error when trying to connect
    console.log(err);
});

The connect function returns a Promise that resolves to a RosApiMenu object so we can run our commands.

Disconnecting

Disconnecting couldn't be more simplier than using the disconnect() function from the RouterOSClient object, or one of the following aliases:

  • close()
  • end()
  • disconnect()

disconnect(): Promise<RouterOSAPI>

Example:

routeros.connect().then((client) => {
    // Connected successfully
    // Let's disconnect
    routeros.disconnect();

    // Or we can check if it was succesfull using the promise
    routeros.disconnect().then(() =>{
        // Disconnected successfully
    }).catch((err) =>{
        // Error trying to disconnect
        console.log(err);
    });
}).catch((err) => {
    // Error when trying to connect
    console.log(err);
});

After disconnecting, you can reconnect using the connect() function without needing to recreate the object. Or you can recreate the object if you want, it's up to you.

Changing credentials

If you need to change any options of the connection, you can do it by using the setOptions() method:

setOptions(options: IRosOptions): RouterOSClient

routeros.connect().then((client1) => {
    // Connected successfully
    routeros.setOptions({
        user: "user1",
        password: ""
    }).close().then(() => {
        return routeros.connect();
    }).then((client2) => {
        // Reconnected with new options
        return routeros.close();
    }).then(() => {
        // Closed second connection
    }).catch((err) => {
        // Error closing connection
        console.log(err);
    });

}).catch((err) => {
    // Error when trying to connect
    console.log(err);
});

Note: if you do end up changing the options, you must reconnect in order to apply the changes.

Retrieving another client

In case you need to get another client beside the one that is returned from the promise when connecting, you can get a new one by using the api() function from the RouterOSClient object you created:

api(): RosApiMenu

routeros.connect().then((client) => {
    // Connected successfully

    // The promise returns a client
    console.log(client);

    // Getting another client like the 
    // one returned from the promise
    const anotherClient = routeros.api();

    console.log(client === anotherClient); // false
}).catch((err) => {
    // Error when trying to connect
    console.log(err);
});

Error event after connected

To get any error after you are already connected, you need to listen to the error event:

routeros.on('error', (err) => {
    console.log(err); // Some error that ocurred when already connected
});

Next: Client Commands