Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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
-------
Expand Down
4 changes: 2 additions & 2 deletions iw_parse → iw_parse/iw_parse
100755 → 100644
Original file line number Diff line number Diff line change
@@ -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. """
Expand Down
14 changes: 9 additions & 5 deletions iw_parse.py → iw_parse/iw_parse.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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'))
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[build-system]
requires = [
"setuptools",
"wheel"
]
build-backend = "setuptools.build_meta"
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
license_files = license.txt
42 changes: 42 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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
)