PDA

View Full Version : Parsing linux command 'iwlist' with Qt



MarkoSan
14th September 2016, 09:39
I am working on some network info application, I've tried Qt Bearer Management, however, I do no get all needed info. So, I've moved to QProcess and through it I inwoke linux command iwlist scan. Then I read all stdout info and it works perfectly. Here is my example `iwlist scan` stdout:

lo Interface doesn't support scanning.

wlan0 Scan completed :
Cell 01 - Address: 64:6E:EA:22:21:D4
Channel:1
Frequency:2.412 GHz (Channel 1)
Quality=38/70 Signal level=-72 dBm
Encryption key:on
ESSID:"heer"
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s
24 Mb/s; 36 Mb/s; 54 Mb/s
Bit Rates:6 Mb/s; 9 Mb/s; 12 Mb/s; 48 Mb/s
Mode:Master
Extra:tsf=000001d13478be74
Extra: Last beacon: 11840ms ago
IE: Unknown: 000468656572
IE: Unknown: 010882848B962430486C
IE: Unknown: 030101
IE: Unknown: 2A0100
IE: Unknown: 32040C121860
IE: IEEE 802.11i/WPA2 Version 1
Group Cipher : TKIP
Pairwise Ciphers (2) : CCMP TKIP
Authentication Suites (1) : PSK
IE: Unknown: 0B0501004B0000
IE: Unknown: 46053208010000
IE: Unknown: 2D1ABC081BFFFF000000000000000000000000000000000000 000000
IE: Unknown: 3D1601080400000000000000000000000000000000000000
IE: Unknown: 7F080400080000000040
IE: Unknown: DD090010180201000C0000
IE: WPA Version 1
Group Cipher : TKIP
Pairwise Ciphers (2) : CCMP TKIP
Authentication Suites (1) : PSK
IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F 00
Cell 02 - Address: 00:23:CD:1D:BB:5C
Channel:4
Frequency:2.427 GHz (Channel 4)
Quality=31/70 Signal level=-79 dBm
Encryption key:on
ESSID:"TP-LINK_1DBB5C"
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
9 Mb/s; 12 Mb/s; 18 Mb/s
Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
Mode:Master
Extra:tsf=000000235998fd80
Extra: Last beacon: 12372ms ago
IE: Unknown: 000E54502D4C494E4B5F314442423543
IE: Unknown: 010882848B960C121824
IE: Unknown: 030104
IE: Unknown: 050400010000
IE: Unknown: 2A0100
IE: IEEE 802.11i/WPA2 Version 1
Group Cipher : CCMP
Pairwise Ciphers (1) : CCMP
Authentication Suites (1) : PSK
IE: WPA Version 1
Group Cipher : CCMP
Pairwise Ciphers (1) : CCMP
Authentication Suites (1) : PSK
IE: Unknown: 32043048606C
IE: Unknown: DD180050F2020101860003A4000027A4000042435E0062322F 00
IE: Unknown: DD1E00904C334E111BFF000000000000000000000000000000 00000000000000
IE: Unknown: 2D1A4E111BFF00000000000000000000000000000000000000 000000
IE: Unknown: DD1A00904C34040D0800000000000000000000000000000000 000000
IE: Unknown: 3D16040D0800000000000000000000000000000000000000
IE: Unknown: DD0900037F01010000FF7F
IE: Unknown: DD0A00037F04010000004000
Cell 03 - Address: 14:CC:20:F6:80:0A
Channel:11
Frequency:2.462 GHz (Channel 11)
Quality=24/70 Signal level=-86 dBm
Encryption key:on
ESSID:"P_wifi"
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
9 Mb/s; 12 Mb/s; 18 Mb/s
Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
Mode:Master
Extra:tsf=0000000e8cd44180
Extra: Last beacon: 11964ms ago
IE: Unknown: 0006505F77696669
IE: Unknown: 010882848B960C121824
IE: Unknown: 03010B
IE: Unknown: 050400010000
IE: Unknown: 0706534920010D14
IE: Unknown: 2A0100
IE: IEEE 802.11i/WPA2 Version 1
Group Cipher : CCMP
Pairwise Ciphers (1) : CCMP
Authentication Suites (1) : PSK
IE: Unknown: 32043048606C
IE: Unknown: 2D1A6E1103FF00000000000000000000000000000000000000 000000
IE: Unknown: 3D160B0F0600000000000000000000000000000000000000
IE: WPA Version 1
Group Cipher : CCMP
Pairwise Ciphers (1) : CCMP
Authentication Suites (1) : PSK
IE: Unknown: DD180050F2020101030003A4000027A4000042435E0062322F 00
IE: Unknown: DD1E00904C336E1103FF000000000000000000000000000000 00000000000000
IE: Unknown: DD1A00904C340B0F0600000000000000000000000000000000 000000
IE: Unknown: DD0900037F01010000FF7F
IE: Unknown: DD3F0050F204104A0001101044000102104700100000000000 001000000014CC20F68010103C000101104900140024E26002 000101600000020001600100020001

eth0 Interface doesn't support scanning.
Current, I have three network interfaces:

lo
wlan0
eth0

Now, from this list I would like to create QMap with key interfaceName(lo, wlan, eth0, ...) and value of QPair with key - parameter name (SSID, channel, frequency, quality, signal level, encryption type, mac address) and as value parameter value itself. If some network interface is NOT WiFi/BroadBand, it can be ignored - not added to QMap at all. How do I parse this output to place fetched info into QMap(QString, QList<QPair<QString, QString>>)? I am working on Qt Desktop Opensource 5.7 for Linux 64bit.

wysota
15th September 2016, 07:48
Read the output line by line. When you detect an interface name (e.g. by indentation) all following lines up to the next interface name will be containing info about that interface. Indentations mark the structure (more indented lines are submaps of the previous line). Split the lines by first colon and you'll have keys and values. If a line doesn't have a colon, append it to the value of the previous key.

anda_skoa
15th September 2016, 09:30
Parsing text data should be the last resort.

If you can I would suggest to research which APIs the tool is using to get the data and try to call those yourself.

There could also be D-Bus API, e.g. in systemd or NetworkManager, that allows to query for some of that.

Cheers,
_