Mikrotik node is now configured through configuration node
This commit is contained in:
@@ -1,16 +1,25 @@
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType('mikrotik-device',{
|
||||
category: 'config',
|
||||
defaults: {
|
||||
host: { value: '192.168.0.1', required: true },
|
||||
port: { value: 8728, required: true, validate: RED.validators.number() },
|
||||
username: { value: 'admin', required: true },
|
||||
password: { value: '', required: true },
|
||||
},
|
||||
label: function() {
|
||||
return this.username + '@' + this.host + ':' + this.port;
|
||||
}
|
||||
});
|
||||
|
||||
RED.nodes.registerType('mikrotik',{
|
||||
category: 'function',
|
||||
color: '#E9967A',
|
||||
defaults: {
|
||||
device: { value: '', type: "mikrotik-device" },
|
||||
name: {value:""},
|
||||
ip : {value: ""},
|
||||
action: {value:"0"}
|
||||
},
|
||||
credentials: {
|
||||
login: {type:"text"},
|
||||
pass: {type:"password"}
|
||||
},
|
||||
inputs:1,
|
||||
outputs:1,
|
||||
icon: "feed.png",
|
||||
@@ -18,22 +27,33 @@
|
||||
return this.name||"mikrotik";
|
||||
}
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-template-name="mikrotik-device">
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-host"><i class="fa fa-server"></i> Host</label>
|
||||
<input type="text" id="node-config-input-host">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-port"><i class="fa fa-server"></i> Port</label>
|
||||
<input type="text" id="node-config-input-port">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-username"><i class="fa fa-user"></i> Username</label>
|
||||
<input type="text" id="node-config-input-username">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-password"><i class="fa fa-key"></i> Password</label>
|
||||
<input type="password" id="node-config-input-password">
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-template-name="mikrotik">
|
||||
<div class="form-row">
|
||||
<label for="node-input-ip"><i class="fa fa-wifi"></i> IP</label>
|
||||
<input type="text" id="node-input-ip" placeholder="xxx.xxx.xxx.xxx">
|
||||
<label for="node-input-device"><i class="fa fa-server"></i> Device</label>
|
||||
<select id="node-input-device"></select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-login"><i class="icon-user"></i> Login</label>
|
||||
<input type="text" id="node-input-login" placeholder="login">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-pass"><i class="fa fa-key"></i> Pass</label>
|
||||
<input type="password" id="node-input-pass" placeholder="password">
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<label for="node-input-action"><i class="icon-tasks"></i> Action</label>
|
||||
<select id="node-input-action" placeholder="action">
|
||||
|
||||
51
mikrotik.js
51
mikrotik.js
@@ -4,44 +4,56 @@
|
||||
var mikrotik = require('mikronode-ng');
|
||||
|
||||
module.exports = function(RED) {
|
||||
function NodeMikrotikDevice(n) {
|
||||
RED.nodes.createNode(this, n);
|
||||
this.host = n.host;
|
||||
this.port = n.port;
|
||||
this.username = n.username;
|
||||
this.password = n.password;
|
||||
}
|
||||
RED.nodes.registerType("mikrotik-device", NodeMikrotikDevice);
|
||||
|
||||
|
||||
function NodeMikrotik(config) {
|
||||
RED.nodes.createNode(this,config);
|
||||
this.action = config.action;
|
||||
this.ip = config.ip;
|
||||
|
||||
this.device = RED.nodes.getNode(config.device);
|
||||
this.action = config.action;
|
||||
|
||||
var node = this;
|
||||
var ip = node.ip;
|
||||
var login = node.credentials.login;
|
||||
var pass = node.credentials.pass;
|
||||
var action;
|
||||
var host = this.device.host;
|
||||
var port = this.device.port;
|
||||
var username = this.device.username;
|
||||
var password = this.device.password;
|
||||
|
||||
var command;
|
||||
|
||||
switch (parseInt(node.action)) {
|
||||
case 0:
|
||||
action = '/log/print';
|
||||
command = '/log/print';
|
||||
break;
|
||||
case 1:
|
||||
action = '/system/resource/print';
|
||||
command = '/system/resource/print';
|
||||
break;
|
||||
case 2:
|
||||
action = '/interface/wireless/registration-table/print';
|
||||
command = '/interface/wireless/registration-table/print';
|
||||
break;
|
||||
case 3:
|
||||
action = '/system/reboot';
|
||||
command = '/system/reboot';
|
||||
break;
|
||||
case 9:
|
||||
action = '';
|
||||
command = '';
|
||||
break;
|
||||
}
|
||||
|
||||
var connection = null;
|
||||
|
||||
this.on('input', function(msg) {
|
||||
if (action == '') action = msg.payload;
|
||||
if(action == '') return false;
|
||||
connection = mikrotik.getConnection(ip, login, pass, {closeOnDone : true});
|
||||
if (command == '') command = msg.payload;
|
||||
if(command == '') return false;
|
||||
connection = mikrotik.getConnection(host, username, password, {closeOnDone : true, port: port});
|
||||
connection.getConnectPromise().then(function(conn) {
|
||||
conn.getCommandPromise(action).then(function resolved(values) {
|
||||
conn.getCommandPromise(command).then(function resolved(values) {
|
||||
var parsed = mikrotik.parseItems(values);
|
||||
var pl = [];
|
||||
parsed.forEach(function(item) {
|
||||
@@ -50,7 +62,7 @@ module.exports = function(RED) {
|
||||
msg.payload = values;
|
||||
node.send(msg);
|
||||
}, function rejected(reason) {
|
||||
node.error('Error executing cmd['+action+']: ' + JSON.stringify(reason));
|
||||
node.error('Error executing cmd['+command+']: ' + JSON.stringify(reason));
|
||||
});
|
||||
},
|
||||
function(err) {
|
||||
@@ -64,12 +76,7 @@ module.exports = function(RED) {
|
||||
});
|
||||
}
|
||||
|
||||
RED.nodes.registerType("mikrotik", NodeMikrotik, {
|
||||
credentials: {
|
||||
login: {type:"text"},
|
||||
pass: {type:"password"}
|
||||
}
|
||||
});
|
||||
RED.nodes.registerType("mikrotik", NodeMikrotik);
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user