-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwlc-plugin.js
117 lines (95 loc) · 3.29 KB
/
wlc-plugin.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env node
// Cisco WLC Wlan Admin state toggle
// Copyright (c) 2024 Tamas Simon
// Dependencies to be installed: node-ssh
// npm install node-ssh
// config wlan <enable|disable> <wlan id>
// Params:
// controller_ip: string
// username: string
// password: string
// enable: boolean
// wlan_id: string
var JSONStream = require('pixl-json-stream');
const {NodeSSH} = require('node-ssh');
const WLAN_ID_MIN = 1;
const WLAN_ID_MAX = 512;
const IPV4REGEX = new RegExp('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$');
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
function streamToString (stream) {
const chunks = [];
return new Promise((resolve, reject) => {
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
stream.on('error', (err) => reject(err));
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
})
}
// setup stdin / stdout streams
process.stdin.setEncoding('utf8');
process.stdout.setEncoding('utf8');
var stream = new JSONStream( process.stdin, process.stdout );
stream.on('json', async function(job) {
// got job from parent
var params = job.params;
// Check if supplied controller_ip is a correct IPv4 address
if (!IPV4REGEX.test(params.controller_ip)) {
stream.write({ complete: 1, code: 1, description: "Supplied Controller IP is not an IPv4 address. Got: '" + (params.controller_ip) + "'" });
return;
}
var controller_ip = params.controller_ip;
// Check if wlan_id is Number
if (isNaN(params.wlan_id)) {
stream.write({ complete: 1, code: 1, description: "Supplied WLAN ID is not a number. Got: '" + (params.wlan_id) + "'" });
return;
}
// Check if wlan_id is between 1-512
var wlan_id = parseInt(params.wlan_id);
if (wlan_id < WLAN_ID_MIN || wlan_id > WLAN_ID_MAX) {
stream.write({ complete: 1, code: 1, description: "Supplied WLAN ID is not within the specified range ( " + WLAN_ID_MIN + " - " + WLAN_ID_MAX + " ). Got: " + (wlan_id) });
return;
}
const ssh = new NodeSSH();
await ssh.connect({
host: controller_ip,
username: params.username,
port: 22,
password: params.password,
});
// Check if connection is successful
if (!ssh.isConnected()) {
stream.write({ complete: 1, code: 1, description: "SSH Connection timed out." });
return;
}
// Execute command
var command = "config wlan " + (params.enable ? "enable" : "disable") + " " + wlan_id;
console.log("Executing: '" + command + "'\n");
var response;
try {
await ssh.withShell(async (connection) => {
await sleep(500);
// Send command
connection.write(command + '\n');
// Collect response
response = streamToString(connection.stdout);
await sleep(500);
});
} catch (error) {
stream.write({ complete: 1, code: 1, description: "Error executing the command. Reason: " + error });
ssh.dispose();
return;
}
// End SSH connection
ssh.dispose();
const result = await response;
console.log(result);
// Send success message
if (result.includes("Request failed - already in the requested state.")) {
stream.write({ complete: 1, code: 0, description: "Command executed successfully, but no change."});
} else {
stream.write({ complete: 1, code: 0, description: "Command executed successfully."});
}
} ); // stream