diff --git a/README.md b/README.md index 4f33936..1d7f373 100644 --- a/README.md +++ b/README.md @@ -3,17 +3,40 @@ iw_parse Parse the output of iwlist scan to get the name, address, quality, channel, and encryption type of all networks broadcasting within your Wireless NIC's reach. +Verified working in Python 3.7 and higher. If you need python2 support, go back to the v0.0.4 release. + Dependencies ------------ -* [pip](http://www.pip-installer.org/en/latest/installing.html "pip installation guide") - If you don't have pip installed, followed the link. +* [python3](https://www.python.org) - Version 3.7.x or higher + +* [pip](https://pip.pypa.io/en/latest/installing/ "pip installation guide") - If you don't have pip installed, follow this link. You should be able to install is this way as well: + +```bash +sudo apt install python3-pip +``` +* [wireless-tools](https://packages.ubuntu.com/focal/wireless-tools) - provides the Linux utility `iwlist`. If not installed, you can run the following on Ubuntu/Raspberry Pi OS/Debian-based systems: + +```bash +sudo apt install wireless-tools +``` Installation ------------ +Recommended - using pip: + ```bash -pip install iw_parse +sudo -H python3 -m pip install --upgrade iw_parse +``` + +Alternative - manually with git: +```bash +git clone https://github.com/cuzzo/iw_parse.git +cd iw_parse +sudo -H python3 -m pip install --upgrade build setuptools +sudo -H python3 -m pip install . ``` Usage @@ -45,10 +68,35 @@ wireless7 A0:21:B7:5F:84:B0 44 % 11 WEP wireless8 04:A1:51:18:E8:E0 41 % 6 WPA v.1 ``` -Example from Python shell: +Example from Python 3 shell: + +```python +>>> from iw_parse import iw_parse +>>> from pprint import pprint +>>> networks = iw_parse.get_interfaces(interface='wlan0') +>>> pprint(networks) +[{'Address': 'F8:1E:DF:F9:B0:0B', + 'Channel': '3', + 'Encryption': 'WEP', + 'Name': 'Francis', + 'Bit Rates': '144 Mb/s', + 'Signal Level': '42', + 'Name': 'Francis', + 'Quality': '100'}, + {'Address': '86:1B:5E:33:17:D4', + 'Channel': '6', + 'Encryption': 'Open', + 'Bit Rates': '54 Mb/s', + 'Signal Level': '72', + 'Name': 'optimumwifi', + 'Quality': '100'}, + ... +``` + +Example from (legacy) Python 2 shell: ```python ->>> import iw_parse +>>> from iw_parse import iw_parse >>> networks = iw_parse.get_interfaces(interface='wlan0') >>> print networks [{'Address': 'F8:1E:DF:F9:B0:0B', @@ -73,6 +121,7 @@ Acknowledgements ---------------- * The vast majority of iw_parse was written by Hugo Chargois. +* pip installation scripts and Python 3 compatiblity written by [Kyle Krattiger](https://gitlab.com/mrmusic25) License ------- diff --git a/iw_parse b/iw_parse/iw_parse old mode 100755 new mode 100644 similarity index 85% rename from iw_parse rename to iw_parse/iw_parse index 022997e..30dcb2c --- a/iw_parse +++ b/iw_parse/iw_parse @@ -1,8 +1,8 @@ -#! /usr/bin/env python +#!/usr/bin/env python3 import sys -from iw_parse import get_parsed_cells, print_cells +from iw_parse.iw_parse import get_parsed_cells, print_cells def main(): """ Pretty prints the output of iwlist scan into a table. """ diff --git a/iw_parse.py b/iw_parse/iw_parse.py similarity index 94% rename from iw_parse.py rename to iw_parse/iw_parse.py index 9be9007..0c15593 100644 --- a/iw_parse.py +++ b/iw_parse/iw_parse.py @@ -1,6 +1,6 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 -# Hugo Chargois - 17 jan. 2010 - v.0.1 +# Hugo Chargois - 17 Mar. 2022 - v0.0.5 # Parses the output of iwlist scan into a table # You can add or change the functions to parse the properties @@ -319,7 +319,9 @@ def call_iwlist(interface='wlan0'): @return string properties: iwlist output """ - return subprocess.check_output(['iwlist', interface, 'scanning']) + s = subprocess.run("iwlist " + str(interface) + " scanning",shell=True,capture_output=True) + #return subprocess.check_output(['iwlist', interface, 'scanning']) + return s.stdout.decode('utf-8') # Note: This method means python3.7.x< is required now def get_interfaces(interface="wlan0"): """ Get parsed iwlist output @@ -333,5 +335,7 @@ def get_interfaces(interface="wlan0"): @return dict properties: dictionary of iwlist attributes """ - return get_parsed_cells(call_iwlist(interface).split('\n')) - + try: + return get_parsed_cells(call_iwlist(interface).split('\n')) + except TypeError: + return get_parsed_cells(call_iwlist(interface).decode('utf-8').split('\n')) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..8d91941 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,6 @@ +[build-system] +requires = [ + "setuptools", + "wheel" +] +build-backend = "setuptools.build_meta" diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..af49403 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[metadata] +license_files = license.txt \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..2ede694 --- /dev/null +++ b/setup.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +# Hugo Chargois - 17 Mar. 2022 - v0.0.5 +# setup.py written by Kyle Krattiger (gitlab.com/mrmusic25) + +# Script to allow installation with pip + +import setuptools + +with open("README.md", "r") as fh: + long_description = fh.read() + +params = { + "scripts" : ['iw_parse/iw_parse'] +} + +setuptools.setup( + name='iw_parse', + version='0.0.5', + description='iwlist scan for Python', + long_description=long_description, + long_description_content_type="text/markdown", + url='https://github.com/cuzzo/iw_parse', + author='Hugo Chargois', + license='BSD-2-Clause', + packages=['iw_parse'], + classifiers=[ + 'Topic :: System :: Operating System Kernels :: Linux', + 'Topic :: System :: Monitoring', + 'Topic :: System :: Networking', + 'Environment :: Console', + 'License :: OSI Approved :: BSD License', + 'Development Status :: 5 - Production/Stable', + 'Programming Language :: Python', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', + ], + **params +) \ No newline at end of file