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 Nov 24, 2017 · 8 revisions

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; 
    
    // The language used for throwing errors, you can find supported languages 
    // here: https://github.com/aluisiora/node-routeros/tree/master/locales
    locale?: string; 
}

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

implementing, will update here when available.

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);
});

Next: Client Operations

Clone this wiki locally