### Simplifying Mikrotik Interface Management with Synchronous Channels (JavaScript) Source: https://github.com/trakkasure/mikronode/blob/release/Readme.md This snippet demonstrates a simplified approach to Mikrotik API communication using `mikronode`'s synchronous channel capabilities. It shows how to execute a sequence of commands (setting and getting interface properties) on a single channel in a synchronous manner, reducing callback nesting, while maintaining a separate channel for real-time interface change listening. Dependencies include the `mikronode` library. ```javascript var MikroNode = require('mikronode'); var device = new MikroNode('192.168.0.1'); device.connect().then(([login])=>login('admin','password')).then(function(conn) { conn.closeOnDone(true); // All channels need to complete before the connection will close. var listenChannel=conn.openChannel(); listenChannel.write('/interface/listen'); // Each sentence that comes from the device goes through this. listenChannel.read.subscribe(function(data) { var packet=MikroNode.resultsToObj(data); console.log('Interface change: '+JSON.stringify(packet)); }); var actionChannel=conn.openChannel(); actionChannel.sync(true); // These will run synchronsously actionChannel.write('/interface/set',{'disabled':'yes','.id':'ether1'}); actionChannel.write('/interface/set',{'disabled':'no','.id':'ether1'}); actionChannel.write('/interface/getall'); actionChannel.on('done',function(packet) { packet.data.forEach(function(data) { var packets=MikroNode.resultsToObj(data); 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. }); ``` -------------------------------- ### Adding IP Address to Mikrotik Interface (JavaScript) Source: https://github.com/trakkasure/mikronode/blob/release/Readme.md This example shows how to connect to a Mikrotik device, log in, and then add an IP address to a specific interface (ether1) using a channel. It includes error handling for 'trap' events and a 'done' event listener to confirm command completion. Finally, it closes both the channel and the connection. Requires the 'mikronode' package. ```javascript var api = require('mikronode'); var device = new api('192.168.0.1'); device.connect().then(([login])=>login('admin','password')).then(function(conn) { var chan=conn.openChannel(); chan.write('/ip/address/add',{'interface':'ether1','address':'192.168.1.1'}); chan.on('trap',function(data) { console.log('Error setting IP: '+data); }); chan.on('done',function(data) { console.log('IP Set.'); }); chan.close(); conn.close(); }); ``` -------------------------------- ### Connecting to Mikrotik and Querying IP Addresses and Firewall Connections (JavaScript) Source: https://github.com/trakkasure/mikronode/blob/release/Readme.md This snippet demonstrates how to connect to a Mikrotik device using the 'mikronode' library, log in, and then open multiple channels to query IP addresses and firewall connections. It illustrates two methods for handling results: using event listeners ('chan.on("done", ...)') and using RxJS-style subscriptions ('chan2.done.subscribe(...)'), including how to convert raw results to objects. It also shows how to properly close channels and the main connection. ```JavaScript var MikroNode = require('mikronode'); var device = new MikroNode('192.168.0.1'); device.connect() .then(([login])=>{ return login('username','password'); }) .then(function(conn) { var chan=conn.openChannel("addresses"); // open a named channel var chan2=conn.openChannel("firewall_connections",true); // open a named channel, turn on "closeOnDone" chan.write('/ip/address/print'); chan.on('done',function(data) { // data is all of the sentences in an array. data.forEach(function(item) { console.log('Interface/IP: '+item.data.interface+"/"+item.data.address); }); chan.close(); // close the channel. It is not autoclosed by default. conn.close(); // when closing connection, the socket is closed and program ends. }); chan2.write('/ip/firewall/print'); chan2.done.subscribe(function(data){ // data is all of the sentences in an array. data.forEach(function(item) { var data = MikroNode.resultsToObj(item.data); // convert array of field items to object. console.log('Interface/IP: '+data.interface+"/"+data.address); }); }); }); ``` -------------------------------- ### Establishing a Connection to Mikrotik Device (JavaScript) Source: https://github.com/trakkasure/mikronode/blob/release/Readme.md This snippet demonstrates how to establish a connection to a Mikrotik device using the MikroNode library. It initializes a new MikroNode instance with the host and port, then connects and logs in with provided credentials. Upon successful connection, it opens a new channel for command execution. Requires the 'mikronode' package. ```javascript var MikroNode = require('mikronode'); var Device =new MikroNode(host,port); Device.connect().then(([login])=>login('admin','password')).then(function(conn) { var chan=conn.openChannel(); }); ``` -------------------------------- ### Managing Mikrotik Interfaces with Multiple Channels (JavaScript) Source: https://github.com/trakkasure/mikronode/blob/release/Readme.md This snippet demonstrates connecting to a Mikrotik device and performing various interface operations using multiple `mikronode` channels. It shows how to open separate channels for listening to interface changes, setting interface properties, and retrieving all interfaces, managing their execution flow and dependencies through promises and event subscriptions. Dependencies include the `mikronode` library. ```javascript var MikroNode = require('mikronode'); var device = new MikroNode('192.168.0.1'); device.connect().then(([login])=>login('admin','password')).then(function(conn) { conn.closeOnDone(true); // when all channels are "done" the connection should close. var chan1=conn.openChannel("interface_listener"); chan1.write('/interface/listen'); chan1.data.subscribe(function(item) { var packet=MikroNode.resultsToObj(item.data); console.log('Interface change: '+JSON.stringify(packet)); }); // This should be called when the cancel is called below. (trap occurs first, then done) chan1.done.subscribe(function(packet) { // This should output everything that the above outputted. packet.data.forEach(function(data) { var packets=MikroNode.resultsToObj(data); console.log('Interface: '+JSON.stringify(packet)); }); }); var chan2=conn.openChannel('config_interface'); // added closeOnDone option to this call var chan3=conn.openChannel('enable_interface'); // We'll use this later. var chan4=conn.openChannel('getall_interfaces'); chan2.write('/interface/set',{'disabled':'yes','.id':'ether1'}); chan2.done.subscribe(function(items) { // We do this here, 'cause we want channel 4 to write after channel 3 is done. // No need to listen for channel3 to complete if we don't care. chan3.write('/interface/set',{'disabled':'no','.id':'ether1'}); chan4.write('/interface/getall'); // Alternative (legacy) way of caturing when chan4 is done. chan4.on('done',function(packet) { packet.data.forEach(function(data) { var packets=MikroNode.resultsToObj(data); console.log('Interface: '+JSON.stringify(packet)); }); chan1.close(); // This should call the /cancel command to stop the listen. }); }); }); ``` -------------------------------- ### Connecting and Managing Mikrotik Interfaces with MikroNode.js Source: https://github.com/trakkasure/mikronode/blob/release/Readme.md This snippet illustrates a comprehensive workflow for managing Mikrotik interfaces. It establishes a connection, authenticates, sets up a 'listen' channel to subscribe to interface changes, and an 'action' channel to modify interface states (disable/enable) and retrieve all interface details. It showcases the use of Promises for sequential operations and RxJS streams for handling real-time data from the device, ensuring all channels are properly closed upon completion or error. ```javascript var MikroNode = require('mikronode'); var device = new MikroNode('192.168.0.1'); device.connect().then(([login])=>login('admin','password')).then(function(conn) { console.log("Logged in."); conn.closeOnDone(true); // All channels need to complete before the connection will close. var listenChannel=conn.openChannel("listen"); // Each sentence that comes from the device goes through the data stream. listenChannel.data.subscribe(function(data) { // var packet=MikroNode.resultsToObj(data); console.log('Interface change: ',JSON.stringify(data)); },error=>{ console.log("Error during listenChannel subscription",error) // This shouldn't be called. },()=>{ console.log("Listen channel done."); }); // Tell our listen channel to notify us of changes to interfaces. listenChannel.write('/interface/listen').then(result=>{ console.log("Listen channel done promise.",result); }) // Catch shuold be called when we call /cancel (or listenChannel.close()) .catch(error=>console.log("Listen channel rejection:",error)); // All our actions go through this. var actionChannel=conn.openChannel("action",false); // don't close on done... because we are running these using promises, the commands complete before each then is complete. // Do things async. This is to prove that promises work as expected along side streams. actionChannel.sync(false); actionChannel.closeOnDone(false); // Turn off closeOnDone because the timeouts set to allow the mikrotik to reflect the changes takes too long. The channel would close. // These will run synchronsously (even though sync is not set to true) console.log("Disabling interface"); actionChannel.write('/interface/set',{'disabled':'yes','.id':'ether1'}).then(results=>{ console.log("Disable complete."); // when the first item comes in from the listen channel, it should send the next command. const {promise,resolve,reject}=MikroNode.getUnwrappedPromise(); listenChannel.data .take(1) // This is just to prove that it grabbed the first one. .do(d=>console.log("Data:",MikroNode.resultsToObj(d.data))) .subscribe(d=>actionChannel.write('/interface/set',{'disabled':'no','.id':'ether1'}).then(resolve,reject)); return promise; }) .then(results=>{ console.log("Enabled complete."); // return new Promise((r,x)=>setTimeout(r,1000)).then(()=>actionChannel.write('/interface/getall')); const {promise,resolve,reject}=MikroNode.getUnwrappedPromise(); // when the second item comes in from the listen channel, it should send the next command. listenChannel.data .take(1) // This is just to prove that it grabbed the second one. .do(d=>console.log("Data:",MikroNode.resultsToObj(d.data))) .subscribe(d=>actionChannel.write('/interface/getall').then(resolve,reject)); return promise; }) .then(results=>{ var formatted=MikroNode.resultsToObj(results.data); var columns=[".id","name","mac-address","comment"]; var filtered=formatted.map(line=>columns.reduce((p,c)=>{p[c]=line[c];return p},{}); console.log('Interface [ID,Name,MAC-Address]: ',JSON.stringify(filtered,true,4)); }) .catch(error=>{ console.log("An error occurred during one of the above commands: ",error); }) // This runs after all commands above, or if an error occurs. .then(nodata=>{ console.log("Closing everything."); listenChannel.close(true); // This should call the /cancel command to stop the listen. actionChannel.close(); }); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.