Вы находитесь на странице: 1из 7

26/3/2018 MikroNode - MikroTik Wiki

MikroNode
From MikroTik Wiki

Contenido
1 Mikronode
1.1 Instalación
1.2 Características
1.3 TODO
1.4 API
1.4.1 Objeto de conexión
1.4.2 Canal
1.5 Ejemplos
1.5.1 Conéctese a un Mikrotik y agregue una dirección a ether1
1.5.2 Escribir el programa para la conversación de API de ejemplo en Mikrotik Wiki
1.5.3 Simplificando lo anterior reduciendo el número de canales.
1.6 Licencia
1.7 Enlaces externos
1.8 Ver también

Mikronode
Interfaz API Mikrotik asíncrona con todas las funciones (http://nodejs.org) para NodeJS (http://nodejs.org) .

var api = require ('mikronode');

var connection = nueva API ('192.168.0.1', 'admin', 'contraseña');


connection.connect (function (conn) {

var chan = conn.openChannel ();

chan.write ('/ ip / address / print', function () {


chan.on ('done', function (data) {

var parsed = api.parseItems (datos);

https://wiki.mikrotik.com/wiki/MikroNode 1/7
26/3/2018 MikroNode - MikroTik Wiki
parsed.forEach (function (item) {
console.log ('Interface / IP:' + item.interface + "/" + item.address);
});

chan.close ();
conn.close ();

});
});
});

Instalación
Clona este repositorio en tu directorio node_modules. - o -

$ npm instalar mikronode

Caracteristicas
Comunicación basada en canales
Se pueden usar múltiples canales a la vez.
Synchronous execution of commands issued on the same channel.
Asynchrounous execution of commands issued on different channels.
Focus on high performance

TODO
Cleanup login section in connect method.
Re-design code to hide internal methods and variables.
Utilize promises.
Write tests to make sure everything keeps working while making above changes.

API
Connection Object

Calling new api(host,user,pass,options) returns a connection object.

The options argument is optional. If specified, it is an object with these fields:


https://wiki.mikrotik.com/wiki/MikroNode 2/7
26/3/2018 MikroNode - MikroTik Wiki

timeout: number of seconds to wait before timing out due to inactivity.


debug: a value between 0 and 5. Greater value means more verbose.
port: alternative port to connect. (In case it's being mapped with a firewall)

conn.connect(callback)

Connect to the target device. The callback function is called after successful login with the current connection object as its parameter.

conn.openChannel(id)

Open and return a new channel object. Each channel is a unique command line to the mikrotik, allowing simultaneous execution of
commands. The ID parameter is optional.

conn.isConnected()

Returns true is currently connected to a mikrotik device.

conn.closeChannel(id)

Closes an open channel. This will call the close method of the channel object.

conn closeOnDone(b)

If b == true, when a done event occurs, close the connection after all channels have been closed.

conn.close(force)

Close the connection. If force is true, force close of any open channels then close this connection.

Channel

The following methods are available for channels:

channel.closeOnDone(b)

If b == true, when a done event occurs, close the channel after all commands queued have been executed.

channel.setSaveBuffer(b)

https://wiki.mikrotik.com/wiki/MikroNode 3/7
26/3/2018 MikroNode - MikroTik Wiki

If b is true, then save each line received in a buffer and pass the entire buffer to the done event. Otherwise the done event will not get
all the lines, only the last line.
This is handy when following trailing output from a listen command, where the data could be endless.

channel.getConnection()
channel.getId()
channel.write(lines,writeCallback)

Lines can be a string, or an array of strings. If it is a string, then it is split on the EOL character and each resulting line is sent as a
separate word (in API speak) If lines is an array, then each element is sent unaltered.

channel.close(force)

Close the channel. If there are any commands still waiting to be executed, they will be completed before closing the channel.
If force is TRUE, then the channel is immediately closed. If the channel is running, the cancel command is sent to stop any running
listen commands, or potentially long running output.

Examples
Connect to a Mikrotik, and add an address to ether1

var api = require('mikronode');

var connection = new api('192.168.0.1','admin','password');


connection.connect(function(conn) {

var chan=conn.openChannel();

chan.write(['/ip/address/add','=interface=ether1','=address=192.168.1.1'],function() {
chan.on('trap',function(data) {
console.log('Error setting IP: '+data);
});
chan.on('done',function(data) {
console.log('IP Set.');
});
chan.close();
conn.close();
});
});

Writing the program for the example API conversation on the Mikrotik Wiki

https://wiki.mikrotik.com/wiki/MikroNode 4/7
26/3/2018 MikroNode - MikroTik Wiki

var api = require('mikronode');

var connection = new api('192.168.0.1','admin','password');


connection.connect(function(conn) {

conn.closeOnDone(true);
var chan2=conn.openChannel(2);
chan2.write('/interface/listen',function(chan) {
chan.on('read',function(data) {
packet=api.parseItems([data])[0];
console.log('Interface change: '+JSON.stringify(packet));
});
});

var chan3=conn.openChannel(3);
chan3.closeOnDone(true);

chan3.write(['/interface/set','=disabled=yes','=.id=ether1'],function(chan) {
chan.on('done',function(d,chan) {
// We do this here, 'cause we want channel 4 to write after channel 3 is done.
var chan4=conn.openChannel(4); // We'll use this later.
chan4.closeOnDone(true);
chan4.write(['/interface/set','=disabled=no','=.id=ether1'],function() {
var chan5=conn.openChannel(5);
chan5.closeOnDone(true);
chan5.write('/interface/getall',function(chan) {
chan.on('done',function(data) {
packets=api.parseItems(data);
packets.forEach(function(packet) {
console.log('Interface: '+JSON.stringify(packet));
});
chan2.close(); // This should call the /cancel command to stop the listen.
});
});
})
});
});
});

Simplifying the above by reducing the number of channels.

Notice how the callback embedding is not needed using the syncronous capability.

var api = require('mikronode');

var connection = new api('192.168.0.1','admin','password');


connection.connect(function(conn) {

conn.closeOnDone(true); // All channels need to complete before the connection will close.
var listenChannel=conn.openChannel();
listenChannel.write('/interface/listen',function(chan) {

https://wiki.mikrotik.com/wiki/MikroNode 5/7
26/3/2018 MikroNode - MikroTik Wiki
chan.on('read',function(data) {
packet=api.parseItems([data])[0];
console.log('Interface change: '+JSON.stringify(packet));
});
});

var actionChannel=conn.openChannel();
// These will run synchronsously
actionChannel.write(['/interface/set','=disabled=yes','=.id=ether1']); // don't care to do anything after it's done.
actionChannel.write(['/interface/set','=disabled=no','=.id=ether1']); // don't care to do anything after it's done.
actionChannel.write('/interface/getall',function(chan) {
chan.on('done',function(data) {
packets=api.parseItems(data);
packets.forEach(function(packet) {
console.log('Interface: '+JSON.stringify(packet));
});
listenChannel.close(); // This should call the /cancel command to stop the listen.
});
});
actionChannel.close(); // The above commands will complete before this is closed.
});

License
(The MIT License)

Copyright (c) 2011 Brandon Myers trakkasure@gmail.com (mailto:trakkasure@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal
in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

EL SOFTWARE SE PROPORCIONA "TAL CUAL", SIN GARANTÍA DE NINGÚN TIPO, EXPRESA O IMPLÍCITA, INCLUIDAS, ENTRE
OTRAS, LAS GARANTÍAS DE COMERCIABILIDAD, IDONEIDAD PARA UN PROPÓSITO EN PARTICULAR Y NO INFRACCIÓN. EN
NINGÚN CASO LOS AUTORES O PROPIETARIOS DE DERECHOS DE AUTOR SERÁN RESPONSABLES DE NINGUNA RECLAMACIÓN,
DAÑOS U OTRA RESPONSABILIDAD, YA SEA EN UNA ACCIÓN CONTRACTUAL, AGRAVIO U OTRO, DERIVADOS, FUERA DEL USO
DEL SOFTWARE O EL USO U OTROS TRATADOS EN EL SOFTWARE.

enlaces externos
NodeJS (http://nodejs.org)

https://wiki.mikrotik.com/wiki/MikroNode 6/7
26/3/2018 MikroNode - MikroTik Wiki

Ver también
API

Retrieved from "https://wiki.mikrotik.com/index.php?title=MikroNode&oldid=21397"

Categoría : API

Esta página fue editada por última vez el 17 de julio de 2011, a las 21:53.

https://wiki.mikrotik.com/wiki/MikroNode 7/7

Вам также может понравиться