Wi-Fi 网络信息解析 - SSID, Channel, BSSID 和 Primary Channel

本文将介绍如何使用 C 语言和正则表达式库从 Wi-Fi 网络信息字符串中提取 SSID、Channel、BSSID 和 Primary Channel 等信息。

Wi-Fi 网络信息字符串示例:

SSID: 'test'
Mode: Managed	RSSI: 0 dBm	SNR: 0 dB	noise: -87 dBm	Channel: 100/160
BSSID: AC:B6:87:29:DF:F1	Capability: ESS ShortPre RRM
Beacon Interval: 100 msecs
Supported Rates: [ 6(b) 9 12(b) 18 24(b) 36 48 54 ]
HE Capable:
    Chanspec: 5GHz channel 114 160MHz (0xe872)
    Primary channel: 100
    HT Capabilities: 40MHz SGI20 SGI40
    Supported HT MCS : 0-31
    Supported VHT MCS:
        NSS1 Tx: 0-11        Rx: 0-11
        NSS2 Tx: 0-11        Rx: 0-11
        NSS3 Tx: 0-11        Rx: 0-11
        NSS4 Tx: 0-11        Rx: 0-11
    Supported HE MCS:
        20/40/80 MHz:
            NSS1 Tx: 0-11        Rx: 0-11
            NSS2 Tx: 0-11        Rx: 0-11
            NSS3 Tx: 0-11        Rx: 0-11
            NSS4 Tx: 0-11        Rx: 0-11
        160 MHz:
            NSS1 Tx: 0-11        Rx: 0-11
            NSS2 Tx: 0-11        Rx: 0-11
            NSS3 Tx: 0-11        Rx: 0-11
            NSS4 Tx: 0-11        Rx: 0-11
    BSS Color: 0x0	Partial BSS Color: false
    BSS Color Disabled: false
    QBSS Channel Utilization: 0x3a (22 %)

C 语言代码示例:

#include <stdio.h>
#include <regex.h>
#include <string.h>

int main() {
    char* pattern_ssid = 'SSID: '([^']+)'';
    char* pattern_channel = 'Channel: ([0-9]+)/';
    char* pattern_bssid = 'BSSID: ([0-9A-Fa-f:]+)';
    char* pattern_primary = 'Primary channel: ([0-9]+)';

    char* str = 'SSID: 'test'
Mode: Managed\tRSSI: 0 dBm\tSNR: 0 dB\tnoise: -87 dBm\tChannel: 100/160
BSSID: AC:B6:87:29:DF:F1\tCapability: ESS ShortPre RRM
Beacon Interval: 100 msecs
Supported Rates: [ 6(b) 9 12(b) 18 24(b) 36 48 54 ]
HE Capable:
    Chanspec: 5GHz channel 114 160MHz (0xe872)
    Primary channel: 100
    HT Capabilities: 40MHz SGI20 SGI40
    Supported HT MCS : 0-31
    Supported VHT MCS:
        NSS1 Tx: 0-11        Rx: 0-11
        NSS2 Tx: 0-11        Rx: 0-11
        NSS3 Tx: 0-11        Rx: 0-11
        NSS4 Tx: 0-11        Rx: 0-11
    Supported HE MCS:
        20/40/80 MHz:
            NSS1 Tx: 0-11        Rx: 0-11
            NSS2 Tx: 0-11        Rx: 0-11
            NSS3 Tx: 0-11        Rx: 0-11
            NSS4 Tx: 0-11        Rx: 0-11
        160 MHz:
            NSS1 Tx: 0-11        Rx: 0-11
            NSS2 Tx: 0-11        Rx: 0-11
            NSS3 Tx: 0-11        Rx: 0-11
            NSS4 Tx: 0-11        Rx: 0-11
    BSS Color: 0x0\tPartial BSS Color: false
    BSS Color Disabled: false
    QBSS Channel Utilization: 0x3a (22 %)';

    regex_t regex_ssid, regex_channel, regex_bssid, regex_primary;
    regmatch_t match_ssid[2], match_channel[2], match_bssid[2], match_primary[2];

    if (regcomp(&regex_ssid, pattern_ssid, REG_EXTENDED) ||
        regcomp(&regex_channel, pattern_channel, REG_EXTENDED) ||
        regcomp(&regex_bssid, pattern_bssid, REG_EXTENDED) ||
        regcomp(&regex_primary, pattern_primary, REG_EXTENDED)) {
            printf('Failed to compile regex pattern\n');
            return 1;
    }

    if (regexec(&regex_ssid, str, 2, match_ssid, 0) ||
        regexec(&regex_channel, str, 2, match_channel, 0) ||
        regexec(&regex_bssid, str, 2, match_bssid, 0) ||
        regexec(&regex_primary, str, 2, match_primary, 0)) {
            printf('Failed to match regex pattern\n');
            return 1;
    }

    char ssid[match_ssid[1].rm_eo - match_ssid[1].rm_so + 1];
    char channel[match_channel[1].rm_eo - match_channel[1].rm_so + 1];
    char bssid[match_bssid[1].rm_eo - match_bssid[1].rm_so + 1];
    char primary[match_primary[1].rm_eo - match_primary[1].rm_so + 1];

    strncpy(ssid, str + match_ssid[1].rm_so, match_ssid[1].rm_eo - match_ssid[1].rm_so);
    ssid[match_ssid[1].rm_eo - match_ssid[1].rm_so] = '\0';

    strncpy(channel, str + match_channel[1].rm_so, match_channel[1].rm_eo - match_channel[1].rm_so);
    channel[match_channel[1].rm_eo - match_channel[1].rm_so] = '\0';

    strncpy(bssid, str + match_bssid[1].rm_so, match_bssid[1].rm_eo - match_bssid[1].rm_so);
    bssid[match_bssid[1].rm_eo - match_bssid[1].rm_so] = '\0';

    strncpy(primary, str + match_primary[1].rm_so, match_primary[1].rm_eo - match_primary[1].rm_so);
    primary[match_primary[1].rm_eo - match_primary[1].rm_so] = '\0';

    printf('SSID: %s\n', ssid);
    printf('Channel: %s\n', channel);
    printf('BSSID: %s\n', bssid);
    printf('Primary channel: %s\n', primary);

    regfree(&regex_ssid);
    regfree(&regex_channel);
    regfree(&regex_bssid);
    regfree(&regex_primary);

    return 0;
}

输出结果:

SSID: test
Channel: 100
BSSID: AC:B6:87:29:DF:F1
Primary channel: 100

注意:

  • 代码中使用的正则表达式需要根据实际的 Wi-Fi 网络信息字符串进行调整。
  • 代码中使用 regcomp 函数编译正则表达式,使用 regexec 函数执行匹配操作,使用 regfree 函数释放正则表达式资源。
  • 代码中使用 strncpy 函数将匹配到的字符串复制到目标字符串数组中。
Wi-Fi 网络信息解析 - SSID, Channel, BSSID 和 Primary Channel

原文地址: https://www.cveoy.top/t/topic/mXg0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录