#!/usr/bin/env python from sys import argv, exit, stderr import os import os.path import xml.etree.ElementTree class AccessPoint: def __init__(self): self.essid = None self.encryption = set() self.packets = 0 self.beaconrate = [] self.bssid = set() self.strength = [] self.manufacturer = set() self.channel = set() def __cmp__(self, other): self = sum(self.strength) / len(self.strength) other = sum(other.strength) / len(other.strength) if self > other: return 1 elif self < other: return -1 else: return 0 def __str__(self): out = "" if self.essid is None: out += "\n" else: out += self.essid + "\n" out += "Encryption: %s\n" % ", ".join(self.encryption) out += "Manufacturer: %s\n" % ", ".join(self.manufacturer) out += "BSSID: %s\n" % ", ".join(self.bssid) out += "Channel: %s\n" % ", ".join(self.channel) out += "Packets: %d" % self.packets if len(self.beaconrate) > 0: out += "\nBeacon Rate: %d" % (sum(self.beaconrate) / len(self.beaconrate)) if len(self.strength) > 0: out += "\nStrength: %d" % (sum(self.strength) / len(self.strength)) if len(self.strength) > 1: out += " max: %d" % max(self.strength) return out def processDir(directory): for entry in os.listdir(directory): entry = os.path.join(directory, entry) if os.path.isdir(entry): processDir(entry) elif os.path.splitext(entry)[1] == ".netxml": processFile(entry) accessPoints = {} def processFile(xmlfile): root = xml.etree.ElementTree.parse(xmlfile).getroot() for network in root.findall("wireless-network"): ap = AccessPoint() essid = network.find("SSID/essid") if essid is None: continue if essid.get("cloaked", "false") == "true": ap.essid = None else: ap.essid = essid.text if ap.essid is not None and ap.essid in accessPoints: ap = accessPoints[ap.essid] for encryption in network.findall("SSID/encryption"): ap.encryption.add(encryption.text) packets = network.findtext("SSID/packets") if packets is not None: ap.packets += int(packets) strength = network.findtext("snr-info/max_signal_dbm") if strength is not None: ap.strength.append(int(strength)) beaconrate = network.findtext("SSID/beaconrate") if beaconrate is not None: ap.beaconrate.append(int(beaconrate)) ap.bssid.add(network.findtext("BSSID")) ap.manufacturer.add(network.findtext("manuf")) ap.channel.add(network.findtext("channel")) accessPoints[ap.essid] = ap if len(argv) < 2: print >> stderr, "Usage: %s DIRECTORIES... | FILES..." % argv[0] exit(-1) for arg in argv[1:]: if not os.path.exists(arg): print >> stderr, "%s does not exist." % arg elif os.path.isfile(arg): processFile(arg) elif os.path.isdir(arg): processDir(arg) accessPoints = sorted(accessPoints.values(), reverse=True) for ap in accessPoints: print ap print