User Tools

Site Tools


wardriving-py

wardriving.py

commented out 'Quality' because it doesn't appear in the dict as a key (why!?)

#!/usr/bin/env python
import subprocess
import re

class line_matcher:
    def __init__(self, regexp, handler):
        self.regexp  = re.compile(regexp)
        self.handler = handler


def handle_new_network(line, result, networks):
    # group(1) is the mac address
    networks.append({})
    networks[-1]['Address'] = result.group(1)

def handle_essid(line, result, networks):
    # group(1) is the essid name
    networks[-1]['ESSID'] = result.group(1)

#def handle_quality(line, result, networks):
#    # group(1) is the quality value
#    # group(2) is probably always 100
#    networks[-1]['Quality'] = result.group(1) + '/' + result.group(2)
                            
def handle_unknown(line, result, networks):
    # group(1) is the key, group(2) is the rest of the line
    networks[-1][result.group(1)] = result.group(2)
    
if __name__ == '__main__':  
    proc = subprocess.Popen(['/sbin/iwlist', 'wlan0', 'scan'],
                            stdout=subprocess.PIPE)
    stdout, stderr =  proc.communicate()

    lines = stdout.split('\n')

    networks = []
    matchers = []

    # catch the line 'Cell ## - Address: XX:YY:ZZ:AA:BB:CC'
    matchers.append(line_matcher(r'\s+Cell \d+ - Address: (\S+)',
                                 handle_new_network))
    
    # catch the line 'ESSID:"network name"
    matchers.append(line_matcher(r'\s+ESSID:"([^"]+)"', 
                                 handle_essid))

    # catch the line 'Quality:X/Y Signal level:X dBm Noise level:Y dBm'
    #matchers.append(line_matcher(r'\s+Quality:(\d+)/(\d+)',
wardriving-py.txt · Last modified: 2012/08/03 16:07 by 94.245.224.234