@@ -4,92 +4,58 @@ import sys
44import subprocess
55import json
66import re
7- import os
87
9- def check_interface_exists (interface ):
10- """Check if the network interface exists """
8+ def get_stations (interface ):
9+ """Get connected stations for an AP interface """
1110 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' ],
11+ result = subprocess .run (['iw' , 'dev' , interface , 'station' , 'dump' ],
2212 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 )
13+ except (subprocess .CalledProcessError , FileNotFoundError ):
2914 return []
3015
3116 stations = []
32- current_station = {}
17+ station = None
3318
34- for line in output .split ('\n ' ):
19+ for line in result . stdout .split ('\n ' ):
3520 line = line .strip ()
3621
3722 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' ,
23+ if station :
24+ stations .append (station )
25+ mac = re . search ( r'([a-fA-F0-9:]{17})' , line )
26+ station = {
27+ 'mac-address' : mac . group ( 1 ) if mac else 'unknown' ,
28+ 'rssi ' : 0 ,
4429 'tx-speed' : 'unknown' ,
4530 'rx-speed' : 'unknown' ,
46- 'rssi' : 0 ,
4731 'connected-time' : 'unknown'
4832 }
4933
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 )
34+ elif station :
35+ if 'signal:' in line and 'avg' not in line :
36+ sig = re .search (r'signal:\s*(-?\d+)' , line )
37+ if sig :
38+ station ['rssi' ] = int (sig .group (1 ))
39+ elif 'tx bitrate:' in line :
40+ bitrate = re .search (r'tx bitrate:\s*(\d+\.?\d*)\s*(MBit/s|Gbit/s)' , line )
41+ if bitrate :
42+ station ['tx-speed' ] = f"{ bitrate .group (1 )} { bitrate .group (2 ).replace ('Bit/s' , 'bps' )} "
43+ elif 'rx bitrate:' in line :
44+ bitrate = re .search (r'rx bitrate:\s*(\d+\.?\d*)\s*(MBit/s|Gbit/s)' , line )
45+ if bitrate :
46+ station ['rx-speed' ] = f"{ bitrate .group (1 )} { bitrate .group (2 ).replace ('Bit/s' , 'bps' )} "
47+ elif 'connected time:' in line :
48+ time = re .search (r'connected time:\s*(\d+\s+\w+)' , line )
49+ if time :
50+ station ['connected-time' ] = time .group (1 )
51+
52+ if station :
53+ stations .append (station )
7654
7755 return stations
7856
79- def main () :
57+ if __name__ == "__main__" :
8058 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 )
8959 sys .exit (1 )
9060
91- stations = parse_iw_station_dump (interface )
92- print (json .dumps (stations , indent = 2 ))
93-
94- if __name__ == "__main__" :
95- main ()
61+ print (json .dumps (get_stations (sys .argv [1 ]), indent = 2 ))
0 commit comments