def resetWirelessDevice(phy, primary_interface): """ Reset a wireless device's interfaces to clean state. This will rename, delete, or add an interface as necessary to make sure only the primary interface exists, e.g. "wlan0" for a wireless device, e.g. phy0. """ primaryExists = False renameOrRemove = list() for ifname in pdos.listdir(SYS_DIR): if ifname in EXCLUDE_IFACES: continue if getWirelessPhyName(ifname) == phy: if ifname == primary_interface: primaryExists = True else: renameOrRemove.append(ifname) for ifname in renameOrRemove: if primaryExists: cmd = ['iw', 'dev', ifname, 'del'] subprocess.call(cmd) else: cmd = ['ip', 'link', 'set', 'dev', ifname, 'down', 'name', primary_interface] subprocess.call(cmd) primaryExists = True if not primaryExists: cmd = ['iw', 'phy', phy, 'interface', 'add', primary_interface, 'type', 'managed'] subprocess.call(cmd)
def flushWirelessInterfaces(phy): """ Remove all virtual interfaces associated with a wireless device. This should be used before giving a chute exclusive access to a device (e.g. monitor mode), so that it does not inherit unexpected interfaces. """ for ifname in pdos.listdir(SYS_DIR): if ifname in EXCLUDE_IFACES: continue if getWirelessPhyName(ifname) == phy: cmd = ['iw', 'dev', ifname, 'del'] subprocess.call(cmd)
def listSystemDevices(): """ Detect devices on the system. The result is a single list of dictionaries, each containing information about a network device. """ devices = list() detectedWifi = set() for ifname in pdos.listdir(SYS_DIR): if ifname in EXCLUDE_IFACES: continue # Only want to detect physical interfaces. if isVirtual(ifname): continue # More special cases to ignore for now. if ifname.startswith("br"): continue if ifname.startswith("docker"): continue if ifname.startswith("sit"): continue dev = { 'name': ifname, 'mac': getMACAddress(ifname) } if isWAN(ifname): dev['type'] = 'wan' elif isWireless(ifname): # Detect wireless devices separately. continue else: dev['type'] = 'lan' devices.append(dev) wifi_devices = listWiFiDevices() devices.extend(wifi_devices) return devices
def listSystemDevices(): """ Detect devices on the system. The result is a single list of dictionaries, each containing information about a network device. """ devices = list() for ifname in pdos.listdir(SYS_DIR): if ifname in EXCLUDE_IFACES: continue # Only want to detect physical interfaces. if isVirtual(ifname): continue # More special cases to ignore for now. if ifname.startswith("br"): continue if ifname.startswith("docker"): continue if ifname.startswith("sit"): continue dev = { 'name': ifname, 'mac': getMACAddress(ifname) } if isWAN(ifname): dev['type'] = 'wan' elif isWireless(ifname): # Detect wireless devices separately. continue else: dev['type'] = 'lan' devices.append(dev) wifi_devices = listWiFiDevices() devices.extend(wifi_devices) return devices
def detectSystemDevices(): """ Detect devices on the system. The result is three lists stored in a dictionary. The three lists are indexed by 'wan', 'wifi', and 'lan'. Other devices may be supported by adding additional lists. Within each list, a device is represented by a dictionary. Currently, only the 'name' field is defined. Later, we may fill in device information (e.g. what channels a WiFi card supports). """ devices = dict() devices['wan'] = list() devices['wifi'] = list() devices['lan'] = list() for ifname in pdos.listdir(SYS_DIR): if ifname in EXCLUDE_IFACES: continue # Only want to detect physical interfaces. if isVirtual(ifname): continue # More special cases to ignore for now. if ifname.startswith("br"): continue if ifname.startswith("docker"): continue dev = {"name": ifname} if isWAN(ifname): devices['wan'].append(dev) elif isWireless(ifname): devices['wifi'].append(dev) else: devices['lan'].append(dev) return devices
def listWiFiDevices(): # Collect information about the physical devices (e.g. phy0 -> MAC address, # device type, PCI slot, etc.) and store as objects in a dictionary. devices = dict() try: for phy in pdos.listdir(IEEE80211_DIR): mac = getPhyMACAddress(phy) reader = SysReader(phy) devices[phy] = { 'name': "wifi{}".format(mac.replace(':', '')), 'type': 'wifi', 'mac': mac, 'phy': phy, 'vendor': reader.getVendorId(), 'device': reader.getDeviceId(), 'slot': reader.getSlotName() } except OSError: # If we get an error here, it probably just means there are no WiFi # devices. pass # Collect a list of interfaces corresponding to each physical device # (e.g. phy0 -> wlan0, vwlan0.0000, etc.) interfaces = dict((dev, []) for dev in devices.keys()) for ifname in pdos.listdir(SYS_DIR): try: path = "{}/{}/phy80211/name".format(SYS_DIR, ifname) phy = readSysFile(path) path = "{}/{}/ifindex".format(SYS_DIR, ifname) ifindex = int(readSysFile(path)) interfaces[phy].append({ 'ifname': ifname, 'ifindex': ifindex }) except: # Error probably means it was not a wireless interface. pass # Sort by ifindex to identify the primary interface, which is the one that # was created when the device was first added. We make use the fact that # Linux uses monotonically increasing ifindex values. for phy, device in six.iteritems(devices): if len(interfaces[phy]) > 0: interfaces[phy].sort(key=operator.itemgetter('ifindex')) device['primary_interface'] = interfaces[phy][0]['ifname'] else: device['primary_interface'] = None # Finally, sort the device list by PCI/USB slot to create an ordering that # is stable across device reboots and somewhat stable across hardware # swaps. result = list(devices.values()) result.sort(key=operator.itemgetter('slot')) pci_index = 0 usb_index = 0 for dev in result: if dev['slot'].startswith("pci"): dev['id'] = "pci-wifi-{}".format(pci_index) pci_index += 1 elif dev['slot'].startswith("usb"): dev['id'] = "usb-wifi-{}".format(usb_index) usb_index += 1 return result
def test_pdos(mOs): pdos.listdir('test') mOs.listdir.assert_called_once_with('test')
def listWiFiDevices(): # Collect information about the physical devices (e.g. phy0 -> MAC address, # device type, PCI slot, etc.) and store as objects in a dictionary. devices = dict() try: for phy in pdos.listdir(IEEE80211_DIR): mac = getPhyMACAddress(phy) reader = SysReader(phy) devices[phy] = { 'name': "wifi{}".format(mac.replace(':', '')), 'type': 'wifi', 'mac': mac, 'phy': phy, 'vendor': reader.getVendorId(), 'device': reader.getDeviceId(), 'slot': reader.getSlotName() } except OSError: # If we get an error here, it probably just means there are no WiFi # devices. pass # Collect a list of interfaces corresponding to each physical device # (e.g. phy0 -> wlan0, vwlan0.0000, etc.) interfaces = dict((dev, []) for dev in devices.keys()) for ifname in pdos.listdir(SYS_DIR): try: path = "{}/{}/phy80211/name".format(SYS_DIR, ifname) phy = readSysFile(path) path = "{}/{}/ifindex".format(SYS_DIR, ifname) ifindex = int(readSysFile(path)) interfaces[phy].append({'ifname': ifname, 'ifindex': ifindex}) except: # Error probably means it was not a wireless interface. pass # Sort by ifindex to identify the primary interface, which is the one that # was created when the device was first added. We make use the fact that # Linux uses monotonically increasing ifindex values. for phy, device in devices.iteritems(): if len(interfaces[phy]) > 0: interfaces[phy].sort(key=operator.itemgetter('ifindex')) device['primary_interface'] = interfaces[phy][0]['ifname'] else: device['primary_interface'] = None # Finally, sort the device list by PCI/USB slot to create an ordering that # is stable across device reboots and somewhat stable across hardware # swaps. result = devices.values() result.sort(key=operator.itemgetter('slot')) pci_index = 0 usb_index = 0 for dev in result: if dev['slot'].startswith("pci"): dev['id'] = "pci-wifi-{}".format(pci_index) pci_index += 1 elif dev['slot'].startswith("usb"): dev['id'] = "usb-wifi-{}".format(usb_index) usb_index += 1 return result