-
Notifications
You must be signed in to change notification settings - Fork 72
/
iwconfig.js
185 lines (166 loc) · 4.66 KB
/
iwconfig.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) 2015 Christopher M. Baker
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
var child_process = require('child_process');
/**
* The **iwconfig** command is used to configure wireless network interfaces.
*
* @private
* @category iwconfig
*
*/
var iwconfig = module.exports = {
exec: child_process.exec,
status: status
};
/**
* Parses the status for a single wireless network interface.
*
* @private
* @static
* @category iwconfig
* @param {string} block The section of stdout for the interface.
* @returns {object} The parsed wireless network interface status.
*
*/
function parse_status_block(block) {
var match;
// Skip out of the block is invalid
if (!block) return;
var parsed = {
interface: block.match(/^([^\s]+)/)[1]
};
if ((match = block.match(/Access Point:\s*([A-Fa-f0-9:]{17})/))) {
parsed.access_point = match[1].toLowerCase();
}
if ((match = block.match(/Frequency[:|=]\s*([0-9\.]+)/))) {
parsed.frequency = parseFloat(match[1]);
}
if ((match = block.match(/IEEE\s*([^\s]+)/))) {
parsed.ieee = match[1].toLowerCase();
}
if ((match = block.match(/Mode[:|=]\s*([^\s]+)/))) {
parsed.mode = match[1].toLowerCase();
}
if ((match = block.match(/Noise level[:|=]\s*(-?[0-9]+)/))) {
parsed.noise = parseInt(match[1], 10);
}
if ((match = block.match(/Link Quality[:|=]\s*([0-9]+)/))) {
parsed.quality = parseInt(match[1], 10);
}
if ((match = block.match(/Sensitivity[:|=]\s*([0-9]+)/))) {
parsed.sensitivity = parseInt(match[1], 10);
}
if ((match = block.match(/Signal level[:|=]\s*(-?[0-9]+)/))) {
parsed.signal = parseInt(match[1], 10);
}
if ((match = block.match(/ESSID[:|=]\s*"([^"]+)"/))) {
parsed.ssid = match[1];
}
if ((match = block.match(/unassociated/))) {
parsed.unassociated = true;
}
return parsed;
}
/**
* Parses the status for all wireless network interfaces.
*
* @private
* @static
* @category iwconfig
* @param {function} callback The callback function.
*
*/
function parse_status(callback) {
return function(error, stdout, stderr) {
if (error) callback(error);
else callback(error,
stdout.trim().replace(/ {10,}/g, '').split('\n\n').map(parse_status_block).filter(function(i) { return !! i }));
};
}
/**
* Parses the status for a single wireless network interface.
*
* @private
* @static
* @category iwconfig
* @param {function} callback The callback function.
*
*/
function parse_status_interface(callback) {
return function(error, stdout, stderr) {
if (error) callback(error);
else callback(error, parse_status_block(stdout.trim()));
};
}
/**
* Parses the status for a single wireless network interface.
*
* @private
* @static
* @category iwconfig
* @param {string} [interface] The wireless network interface.
* @param {function} callback The callback function.
* @example
*
* var iwconfig = require('wireless-tools/iwconfig');
*
* iwconfig.status(function(err, status) {
* console.log(status);
* });
*
* // =>
* [
* {
* interface: 'wlan0',
* access_point: '00:0b:81:95:12:21',
* frequency: 2.437,
* ieee: '802.11bg',
* mode: 'master',
* noise: 0,
* quality: 77,
* sensitivity: 0,
* signal: 50,
* ssid: 'RaspberryPi'
* },
* {
* interface: 'wlan1',
* frequency: 2.412,
* mode: 'auto',
* noise: 0,
* quality: 0,
* sensitivity: 0,
* signal: 0,
* unassociated: true
* }
* ]
*
*/
function status(interface, callback) {
if (callback) {
return this.exec('iwconfig ' + interface,
parse_status_interface(callback));
}
else {
return this.exec('iwconfig', parse_status(interface));
}
}