Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
iwlist scan parser for python
#!/usr/bin/env python
# Based on http://ubuntuforums.org/showthread.php?t=984492&p=6193749#post6193749
import subprocess
import re
proc = subprocess.Popen('iwlist scan 2>/dev/null', shell=True, stdout=subprocess.PIPE, )
stdout_str = proc.communicate()[0]
stdout_list = stdout_str.split('\n')
networks = []
network = {}
for line in stdout_list:
line = line.strip()
match = re.search('Address: (\S+)',line)
if match:
if len(network):
networks.append(network)
network = {}
network["mac"] = match.group(1)
match = re.search('ESSID:"(\S+)"',line)
if match:
network["ssid"] = match.group(1)
# Quality=31/70 Signal level=-79 dBm
match = re.search('Quality=([0-9]+)\/([0-9]+)[ \t]+Signal level=([0-9-]+) dBm',line)
if match:
network["quality"] = match.group(1)
network["quality@scale"] = match.group(2)
network["dbm"] = match.group(3)
# Encryption key:on
match = re.search('Encryption key:(on|.+)',line)
if match:
network["encryption"] = match.group(1)
# Channel:1
match = re.search('Channel:([0-9]+)',line)
if match:
network["channel"] = match.group(1)
# Frequency:2.412 GHz (Channel 1)
match = re.search('Frequency:([0-9\.]+) GHz',line)
if match:
network["freq"] = match.group(1)
# IE: WPA Version 1
# Group Cipher : TKIP
# Pairwise Ciphers (1) : TKIP
# Authentication Suites (1) : PSK
# IE: IEEE 802.11i/WPA2 Version 1
# Group Cipher : TKIP
# Pairwise Ciphers (1) : CCMP
# Authentication Suites (1) : PSK
if len(network):
networks.append(network)
print networks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.