-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathiwlist.py
More file actions
77 lines (57 loc) · 2.36 KB
/
Copy pathiwlist.py
File metadata and controls
77 lines (57 loc) · 2.36 KB
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
# Copyright (C) 2009-2012 Red Hat, Inc.
# Copyright (C) 2013-2014 Nathan Hoad.
#
# Interface with iwlib by Nathan Hoad <nathan@getoffmalawn.com>.
#
# This application is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; version 2.
#
# This application is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
import os
from .utils import _get_range_info, _parse_stats, _get_bytes, iwlib_socket
from ._iwlib import ffi, lib as iwlib
def scan(interface):
"""Perform a scan for access points in the area.
Arguments:
interface - device to use for scanning (e.g. eth1, wlan0).
"""
interface = _get_bytes(interface)
head = ffi.new('wireless_scan_head *')
with iwlib_socket() as sock:
range = _get_range_info(interface, sock=sock)
if iwlib.iw_scan(sock, interface, range.we_version_compiled, head) != 0:
errno = ffi.errno
strerror = "Error while scanning: %s" % os.strerror(errno)
raise OSError(errno, strerror)
results = []
scan = head.result
buf = ffi.new('char []', 1024)
while scan != ffi.NULL:
parsed_scan = {}
if scan.b.has_mode:
parsed_scan['Mode'] = ffi.string(iwlib.iw_operation_mode[scan.b.mode])
if scan.b.has_freq:
parsed_scan['Frequency'] = scan.b.freq
if scan.b.essid_on:
parsed_scan['ESSID'] = ffi.string(scan.b.essid)
else:
parsed_scan['ESSID'] = b'Auto'
if scan.has_ap_addr:
iwlib.iw_ether_ntop(
ffi.cast('struct ether_addr *', scan.ap_addr.sa_data), buf)
if scan.b.has_mode and scan.b.mode == iwlib.IW_MODE_ADHOC:
parsed_scan['Cell'] = ffi.string(buf)
else:
parsed_scan['Access Point'] = ffi.string(buf)
if scan.has_maxbitrate:
iwlib.iw_print_bitrate(buf, len(buf), scan.maxbitrate.value)
parsed_scan['BitRate'] = ffi.string(buf)
if scan.has_stats:
parsed_scan['stats'] = _parse_stats(scan.stats)
results.append(parsed_scan)
scan = scan.next
return results