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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
extern crate regex;
#[doc(no_inline)]
use regex::Regex;
#[allow(missing_docs)]
#[derive(Debug,PartialEq,Eq)]
pub enum Error {
SyntaxRegexError,
CommandNotFound,
NoMatch,
FailedToParse,
NoValue,
}
#[derive(Debug,PartialEq,Eq)]
pub struct Wifi {
pub mac: String,
pub ssid: String,
pub channel: String,
pub signal_level: String,
pub security: String,
}
#[cfg(target_os="macos")]
pub fn scan() -> Result<Vec<Wifi>, Error> {
use std::process::Command;
let output = try!(Command::new("/System/Library/PrivateFrameworks/Apple80211.\
framework/Versions/Current/Resources/airport")
.arg("-s")
.output()
.map_err(|_| Error::CommandNotFound));
let data = String::from_utf8_lossy(&output.stdout);
parse_airport(&data)
}
#[cfg(target_os="linux")]
pub fn scan() -> Result<Vec<Wifi>, Error> {
use std::env;
use std::process::Command;
const PATH_ENV: &'static str = "PATH";
let path_system = "/usr/sbin:/sbin";
let path = env::var_os(PATH_ENV).map_or(path_system.to_string(), |v| {
format!("{}:{}", v.to_string_lossy().into_owned(), path_system)
});
let output = try!(Command::new("iwlist")
.env(PATH_ENV, path)
.arg("scan")
.output()
.map_err(|_| Error::CommandNotFound));
let data = String::from_utf8_lossy(&output.stdout);
parse_iwlist(&data)
}
#[allow(dead_code)]
fn parse_airport(network_list: &str) -> Result<Vec<Wifi>, Error> {
let mut wifis: Vec<Wifi> = Vec::new();
let mut lines = network_list.lines();
let headers = lines.next().unwrap();
let headers_string = String::from(headers);
let col_mac = headers_string.find("BSSID").expect("failed to find BSSID");
let col_rrsi = headers_string.find("RSSI").expect("failed to find RSSI");
let col_channel = headers_string.find("CHANNEL").expect("failed to find CHANNEL");
let col_ht = headers_string.find("HT").expect("failed to find HT");
let col_security = headers_string.find("SECURITY").expect("failed to find SECURITY");
for line in lines {
let ssid = &line[..col_mac].trim();
let mac = &line[col_mac..col_rrsi].trim();
let signal_level = &line[col_rrsi..col_channel].trim();
let channel = &line[col_channel..col_ht].trim();
let security = &line[col_security..].trim();
wifis.push(Wifi {
mac: mac.to_string(),
ssid: ssid.to_string(),
channel: channel.to_string(),
signal_level: signal_level.to_string(),
security: security.to_string(),
});
}
Ok(wifis)
}
#[allow(dead_code)]
fn parse_iwlist(network_list: &str) -> Result<Vec<Wifi>, Error> {
let mut wifis: Vec<Wifi> = Vec::new();
let cell_regex = try!(Regex::new(r"Cell [0-9]{2,} - Address:")
.map_err(|_| Error::SyntaxRegexError));
let mac_regex =
try!(Regex::new(r"([0-9a-zA-Z]{1}[0-9a-zA-Z]{1}[:]{1}){5}[0-9a-zA-Z]{1}[0-9a-zA-Z]{1}")
.map_err(|_| Error::SyntaxRegexError));
for block in cell_regex.split(&network_list) {
let mut lines = block.lines();
let mut wifi_mac = String::new();
let mut wifi_ssid = String::new();
let mut wifi_channel = String::new();
let mut wifi_rssi = String::new();
let wifi_security = String::new();
let mac_matches = mac_regex.captures(try!(lines.next().ok_or(Error::NoValue)));
if let Some(matches) = mac_matches {
if let Some(mac) = matches.at(0) {
wifi_mac = mac.to_string();
}
}
for line in lines {
if line.find("ESSID:").is_some() {
let ssid = line.split(":").nth(1).unwrap_or("").replace("\"", "");
wifi_ssid = ssid.to_string();
} else if line.find("Frequency:").is_some() {
wifi_channel = line.split("Channel")
.nth(1)
.unwrap_or("")
.replace(")", "")
.trim()
.to_string();
} else if line.find("Signal level").is_some() {
if line.find("Quality").is_some() {
wifi_rssi = line.split("Signal level=")
.nth(1)
.unwrap_or("")
.replace("dBm", "")
.trim()
.to_string();
} else {
let re = try!(Regex::new(r"Signal level=(\d+)/100")
.map_err(|_| Error::SyntaxRegexError));
let value_raw = try!(try!(re.captures(line).ok_or(Error::FailedToParse))
.at(1)
.ok_or(Error::NoValue));
let value = try!(value_raw.parse::<i32>().map_err(|_| Error::FailedToParse));
let strength_calc = ((100 * value) / 100) / 2 - 100;
wifi_rssi = strength_calc.to_string();
}
}
if !wifi_ssid.is_empty() && !wifi_mac.is_empty() && !wifi_rssi.is_empty() {
wifis.push(Wifi {
ssid: wifi_ssid.to_string(),
mac: wifi_mac.to_string(),
channel: wifi_channel.to_string(),
signal_level: wifi_rssi.to_string(),
security: wifi_security.to_string(),
});
wifi_ssid = String::new();
wifi_mac = String::new();
wifi_rssi = String::new();
wifi_channel = String::new();
}
}
}
Ok(wifis)
}
#[test]
fn should_parse_iwlist_type_1() {
let mut expected: Vec<Wifi> = Vec::new();
expected.push(Wifi {
mac: "00:35:1A:6F:0F:40".to_string(),
ssid: "TEST-Wifi".to_string(),
channel: "6".to_string(),
signal_level: "-72".to_string(),
security: "".to_string(),
});
expected.push(Wifi {
mac: "00:F2:8B:8F:58:77".to_string(),
ssid: "<hidden>".to_string(),
channel: "11".to_string(),
signal_level: "-71".to_string(),
security: "".to_string(),
});
use std::path::PathBuf;
let mut path = PathBuf::new();
path.push("tests");
path.push("fixtures");
path.push("iwlist");
path.push("iwlist01_ubuntu1404.txt");
let file_path = path.as_os_str();
use std::fs::File;
use std::io::Read;
let mut file = File::open(&file_path).unwrap();
let mut filestr = String::new();
let result = file.read_to_string(&mut filestr).unwrap();
let result = parse_iwlist(&filestr).unwrap();
assert_eq!(expected[0], result[0]);
assert_eq!(expected[1], result[28]);
}
#[test]
fn should_parse_iwlist_type_2() {
let mut expected: Vec<Wifi> = Vec::new();
expected.push(Wifi {
mac: "D4:D1:84:50:76:45".to_string(),
ssid: "gsy-97796".to_string(),
channel: "6".to_string(),
signal_level: "-76".to_string(),
security: "".to_string(),
});
expected.push(Wifi {
mac: "7C:B7:33:AE:3B:05".to_string(),
ssid: "visitor-18170".to_string(),
channel: "9".to_string(),
signal_level: "-70".to_string(),
security: "".to_string(),
});
use std::path::PathBuf;
let mut path = PathBuf::new();
path.push("tests");
path.push("fixtures");
path.push("iwlist");
path.push("iwlist02_raspi.txt");
let file_path = path.as_os_str();
use std::fs::File;
use std::io::Read;
let mut file = File::open(&file_path).unwrap();
let mut filestr = String::new();
let result = file.read_to_string(&mut filestr).unwrap();
let result = parse_iwlist(&filestr).unwrap();
assert_eq!(expected[0], result[0]);
assert_eq!(expected[1], result[2]);
}
#[test]
fn should_parse_airport() {
let mut expected: Vec<Wifi> = Vec::new();
expected.push(Wifi {
mac: "00:35:1a:90:56:03".to_string(),
ssid: "OurTest".to_string(),
channel: "112".to_string(),
signal_level: "-70".to_string(),
security: "WPA2(PSK/AES/AES)".to_string(),
});
expected.push(Wifi {
mac: "00:35:1a:90:56:00".to_string(),
ssid: "TEST-Wifi".to_string(),
channel: "1".to_string(),
signal_level: "-67".to_string(),
security: "WPA2(PSK/AES/AES)".to_string(),
});
use std::path::PathBuf;
let mut path = PathBuf::new();
path.push("tests");
path.push("fixtures");
path.push("airport");
path.push("airport01.txt");
let file_path = path.as_os_str();
use std::fs::File;
use std::io::Read;
let mut file = File::open(&file_path).unwrap();
let mut filestr = String::new();
let result = file.read_to_string(&mut filestr).unwrap();
let result = parse_airport(&filestr).unwrap();
let last = result.len() - 1;
assert_eq!(expected[0], result[0]);
assert_eq!(expected[1], result[last]);
}