|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import subprocess |
| 5 | +import json |
| 6 | +import re |
| 7 | +import os |
| 8 | + |
| 9 | +def check_interface_exists(interface): |
| 10 | + """Check if the network interface exists""" |
| 11 | + try: |
| 12 | + result = subprocess.run(['ip', 'link', 'show', interface], |
| 13 | + capture_output=True, check=True) |
| 14 | + return True |
| 15 | + except subprocess.CalledProcessError: |
| 16 | + return False |
| 17 | + |
| 18 | +def parse_iw_station_dump(interface): |
| 19 | + """Parse iw station dump output and return JSON""" |
| 20 | + try: |
| 21 | + result = subprocess.run(['sudo', 'iw', 'dev', interface, 'station', 'dump'], |
| 22 | + capture_output=True, text=True, check=True) |
| 23 | + output = result.stdout |
| 24 | + except subprocess.CalledProcessError as e: |
| 25 | + print(f"Error running iw command: {e}", file=sys.stderr) |
| 26 | + return [] |
| 27 | + except FileNotFoundError: |
| 28 | + print("Error: 'iw' command not found", file=sys.stderr) |
| 29 | + return [] |
| 30 | + |
| 31 | + stations = [] |
| 32 | + current_station = {} |
| 33 | + |
| 34 | + for line in output.split('\n'): |
| 35 | + line = line.strip() |
| 36 | + |
| 37 | + if line.startswith('Station'): |
| 38 | + if current_station: |
| 39 | + stations.append(current_station) |
| 40 | + |
| 41 | + mac_match = re.search(r'Station ([a-fA-F0-9:]{17})', line) |
| 42 | + current_station = { |
| 43 | + 'mac-address': mac_match.group(1) if mac_match else 'unknown', |
| 44 | + 'tx-speed': 'unknown', |
| 45 | + 'rx-speed': 'unknown', |
| 46 | + 'rssi': 0, |
| 47 | + 'connected-time': 'unknown' |
| 48 | + } |
| 49 | + |
| 50 | + elif 'tx bitrate:' in line: |
| 51 | + bitrate_match = re.search(r'tx bitrate:\s*(\d+\.?\d*)\s*(MBit/s|Gbit/s)', line) |
| 52 | + if bitrate_match: |
| 53 | + speed = bitrate_match.group(1) |
| 54 | + unit = bitrate_match.group(2) |
| 55 | + current_station['tx-speed'] = f"{speed} {unit.replace('Bit/s', 'bps')}" |
| 56 | + |
| 57 | + elif 'rx bitrate:' in line: |
| 58 | + bitrate_match = re.search(r'rx bitrate:\s*(\d+\.?\d*)\s*(MBit/s|Gbit/s)', line) |
| 59 | + if bitrate_match: |
| 60 | + speed = bitrate_match.group(1) |
| 61 | + unit = bitrate_match.group(2) |
| 62 | + current_station['rx-speed'] = f"{speed} {unit.replace('Bit/s', 'bps')}" |
| 63 | + |
| 64 | + elif 'signal:' in line and 'avg' not in line: |
| 65 | + signal_match = re.search(r'signal:\s*(-?\d+)', line) |
| 66 | + if signal_match: |
| 67 | + current_station['rssi'] = int(f"{signal_match.group(1)}") |
| 68 | + |
| 69 | + elif 'connected time:' in line: |
| 70 | + time_match = re.search(r'connected time:\s*(\d+\s+\w+)', line) |
| 71 | + if time_match: |
| 72 | + current_station['connected-time'] = time_match.group(1) |
| 73 | + |
| 74 | + if current_station: |
| 75 | + stations.append(current_station) |
| 76 | + |
| 77 | + return stations |
| 78 | + |
| 79 | +def main(): |
| 80 | + if len(sys.argv) != 2: |
| 81 | + print("Usage: python3 wifi_station_parser.py <interface>") |
| 82 | + print("Example: python3 wifi_station_parser.py wifi0_ap2") |
| 83 | + sys.exit(1) |
| 84 | + |
| 85 | + interface = sys.argv[1] |
| 86 | + |
| 87 | + if not check_interface_exists(interface): |
| 88 | + print(f"Error: Interface '{interface}' not found", file=sys.stderr) |
| 89 | + sys.exit(1) |
| 90 | + |
| 91 | + stations = parse_iw_station_dump(interface) |
| 92 | + print(json.dumps(stations, indent=2)) |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + main() |
0 commit comments