forked from node-red-contrib/node-red-contrib-mikrotik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmikrotik.js
80 lines (67 loc) · 2.31 KB
/
mikrotik.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Created by Bladerunner on 11/03/16.
*/
var mikrotik = require('mikronode-ng');
module.exports = function(RED) {
function NodeMikrotik(config) {
RED.nodes.createNode(this,config);
this.action = config.action;
this.ip = config.ip;
this.action = config.action;
var node = this;
var ip = node.ip;
var login = node.credentials.login;
var pass = node.credentials.pass;
var action;
switch (parseInt(node.action)) {
case 0:
action = '/log/print';
break;
case 1:
action = '/system/resource/print';
break;
case 2:
action = '/interface/wireless/registration-table/print';
break;
case 3:
action = '/system/reboot';
break;
case 9:
action = '';
break;
}
var connection = null;
this.on('input', function(msg) {
var cmd = action;
if (cmd == '') cmd = msg.payload;
if (cmd == '') return false;
connection = mikrotik.getConnection(ip, login, pass, {closeOnDone : true});
connection.getConnectPromise().then(function(conn) {
conn.getCommandPromise(cmd).then(function resolved(values) {
var parsed = mikrotik.parseItems(values);
var pl = [];
parsed.forEach(function(item) {
pl.push(item);
});
msg.payload = values;
node.send(msg);
}, function rejected(reason) {
node.error('Error executing cmd['+action+']: ' + JSON.stringify(reason));
});
},
function(err) {
node.error("Connection error: " + err);
}
);
});
this.on('close', function() {
connection && connection.connected && connection.close(true);
});
}
RED.nodes.registerType("mikrotik", NodeMikrotik, {
credentials: {
login: {type:"text"},
pass: {type:"password"}
}
});
};