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