awkman / pywifi

A cross-platform module for manipulating WiFi devices.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

On OSX: NameError: name 'wifiutils' is not defined

kootenpv opened this issue · comments

>>> import pywifi
>>> iface = pywifi.PyWiFi().interfaces()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pywifi/pywifi.py", line 23, in interfaces
    for interface in wifiutils.interfaces():
NameError: name 'wifiutils' is not defined
commented

Because I don't have any OSX environment to develop, now pywifi doesn't support OSX yet.

So, I only need to scan wifi. I provide code below. I don't really understand why there's so much interface for windows and linux, but would it be possible for you to create a skeleton around this so we can add OSX support? I could then benefit from using pywifi, and anyone after me :)

import subprocess
import os


def get_parser(line):
    bbsid = line.index("BSSID")
    rssi = line.index("RSSI")
    channel = line.index("CHANNEL")
    security = line.index("SECURITY")
    return [("SSID", (0, bbsid)),
            ("BSSID", (bbsid, rssi)),
            ("RSSI", (rssi, channel)),
            ("SECURITY", (security, -1))]


def sample():
    cmd = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s"
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (out, _) = proc.communicate()
    dc = {}
    parser = []
    for line in out.decode("utf8").split("\n"):
        if line.strip().startswith("SSID"):
            parser = get_parser(line)
        elif line:
            row = [line[inds[0]:inds[1]].strip() for name, inds in parser]
            # row[0] is SSID
            # row[1] is BSSID
            # row[2] is RSSI/Signal
            key = row[0] + " " + row[1]
            value = int(row[2])
            dc[key] = value
    return dc

Output:

{
 'H368NDF1690 54:22:f8:XXXXX': -69,
 'KPN Fon XX:22:XX:XX:16:91': -70
}
commented

Now pywifi is still under prototype stage, so I need more time to refactor its structure for maintainability.
I will add osx folder under pywifi folder
you can implement following:

in osx_wifiutil.py file

  • get_interfaces() to return the interface names list
  • scan() to trigger the scan via specified interface and store the scan result in the global scan_results variable
  • scan_results() to return the parsed result

in wifiutils.py file
I already filled the code based on above osx_wifiutil.py utilities. If no big issue, I think you don't
need to modify it.