/
freebsd_route.js
105 lines (92 loc) · 2.67 KB
/
freebsd_route.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
/**
* This driver extracts information about network routes in freebsd os
* The communication protocol is SSH
* This driver create a dynamic monitoring variables for the routes for ip V6 and V4
* Tested under freebsd 12.3-STABLE
*/
var validate_cmd = "netstat -r -n";
var route_v4_cmd = "netstat -4 -r -n | tail -n +5";
var route_v6_cmd = "netstat -6 -r -n | tail -n +5";
var sshConfig = {
username: D.device.username(),
password: D.device.password(),
port: 22,
timeout: 30000
};
var table = D.createTable("Routing table", [
{ label: "Destination" },
{ label: "Gateway" },
{ label: "Flags" },
{ label: "Netif Expire" },
]);
function exec_command(command, callback) {
sshConfig.command = command;
D.device.sendSSHCommand(sshConfig, function (out, err) {
if (err) {
console.error("error while executing command: " + command);
console.error(err);
D.failure();
}
callback(out.split("\n"));
});
}
function route(version, cmd) {
return function (callback) {
exec_command(cmd, function (lines) {
lines.forEach(function (line) {
var route_data = line.split(/\s+/gm);
table.insertRecord(version + ">" + route_data[0], [
route_data[0],
route_data[1],
route_data[2],
route_data[3]
]);
});
callback();
});
};
}
/**
*
* @param {[function]} arrayFn An array of functions that will be executed in the same time, each function should have a callback as parameter
* @param {function} callback This function is called when all functions in arrayFn are done
*/
function execute_all(arrayFn, callback) {
if (arrayFn.length == 0) {
callback([]);
}
var length = arrayFn.length;
var results = new Array(length);
var finished = 0;
arrayFn.forEach(function (fn, index) {
fn(function (result) {
results[index] = result;
if (++finished == length) {
callback(results);
}
});
});
}
/**
* @remote_procedure
* @label Validate Association
* @documentation This procedure is used to validate if the ssh commands are running successfully
*/
function validate() {
exec_command(validate_cmd, function () {
D.success();
});
}
/**
* @remote_procedure
* @label Get Device Variables
* @documentation This procedure is used for retrieving device routing table
*/
function get_status() {
execute_all([
route("ip_v4", route_v4_cmd),
route("ip_v6", route_v6_cmd),
], function () {
D.success(table);
});
}