【全志系统 - WIFI - 4】- STA模式

1. 前言

目前wifi调试,最重要的工具有两个:

    1. wpa_supplicant: WiFi Protected Access,网络安全访问
    1. wireless-tools:

wpa_supplicant 是一个 独立运行的 守护进程,其核心是一个消息循环,在消息循环中处理WPA状态机、控制命令、驱动事件、配置信息等。

wpa_supplicant 重要包括两个可执行工具:

工具 说明 备注
wpa_supplicant wifi服务器 连接wifi,断开wifi,启动热点等
wpa_cli wifi客户端 通过本地socket连接wpa_supplicant,发送命令

本博主要讲解wpa_supplicant的用法!

2. wpa_supplicant

2.1 配置文件

指令cat /etc/wifi/wpa_supplicant.conf

更换wifi可手动修改该配置文件

root@TinaLinux:/usr# cat /etc/wifi/wpa_supplicant.conf
ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=0
update_config=1
ap_scan=1

network={
        ssid="ZZ"
        key_mgmt=WPA-PSK
        psk="officeworking"
        priority=2    ##加了优先级,貌似并没有什么卵用
}

network={
        ssid="Jimmy"
        key_mgmt=WPA-PSK
        psk="12345678"
        priority=1
}

Notice:详情可参考以下连接

wpa_supplicant.conf配置解析-1
wpa_supplicant.conf配置解析-2

选项 说明 备注
ssid 连接的wifi名称
key_mgmt 加密级别 无密码为NONE
psk wifi密码 无密码则无此项
ctrl_interface 控制接口 外部程序打开该控制接口;
从而管理wpa_supplicant;
默认为/var/run/wpa_supplicant
ctrl_interface_group 可控制wpa_supplicant的群组ID 默认为root,ID=0
update_config 是否允许外部更新/覆盖该配置文件 默认是允许的
不允许只需要注释掉(加#)
ap_scan 接入点的扫描和选择
network 连接的路由器信息 可设置多个,按顺序连接;
第一个连接失败,会尝试第二个

2.2 wpa_supplicant 参数信息

选项 说明
-b optional bridge interface name
-B run daemon in the background
-c Configuration file
-C ctrl_interface parameter (only used if -c is not)
-d increase debugging verbosity (-dd even more)
-D driver name (can be multiple drivers: nl80211,wext)
-e entropy file
-g global ctrl_interface
-G global ctrl_interface group
-h show this help text
-i interface name
-I additional configuration file
-K include keys (passwords, etc.) in debug output
-L show license (BSD)
-N start describing new interface
-o override driver parameter for new interfaces
-O override ctrl_interface parameter for new interfaces
-p driver parameters
-P PID file
-q decrease debugging verbosity (-qq even less)
-t include timestamp in debug messages
-v show version
-W wait for a control interface monitor before starting

最常用的指令有如下几个:

  • i:指定端口
  • c:指定配置文件
  • D:指定使用的wifi驱动

如我的系统配置:

eg. wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wpa_supplicant.conf

驱动解释:

  • nl80211 = Linux nl80211/cfg80211
  • wext = Linux wireless extensions (generic)
  • wired = Wired Ethernet driver

接口解释

接口 IP 说明
Lo 127.0.0.1 Local Loopback,虚拟回环接口;
在系统内部接收和发送数据包;
无需驱动程序
br0 None 网桥接口
可以将两个接口进行连接
对帧进行转发
eth0 ABC类网址内网地址 以太网接口与网卡对应
每个MAC对应一个以太网接口
其工作完全由网卡相应的驱动程序控制
WLAN0 同上 无线网卡对应的接口
无线网卡需要对应的驱动程序才能工作

Notice

启动wpa_supplicant之前wifi必须先启动,wpa_supplicnat使用在wifi client端口上
wifi启动命令:ifconfig wlan0 up

2.3 wpa_cli 参数信息

shell中键入 wpa_cli后,进入一个交互界面,可输入如下指令进行对应的操作!
Notice:如果不想进入交互界面,可采用 wpa_cli [command]

指令 缩写 说明
status stat displays the current connection status
disconnect disc prevents wpa_supplicant from connecting to any access point
quit q exits wpa_cli
terminate term kills wpa_supplicant
reconfigure recon reloads wpa_supplicant with the configuration file supplied (-c parameter)
scan scan scans for available access points (only scans it, doesn't display anything)
scan_result scan_r displays the results of the last scan
list_networks list_n displays a list of configured networks and their status (active or not, enabled or disabled)
select_network select_n select a network among those defined to initiate a connection (ie select_network 0)
enable_network enable_n makes a configured network available for selection (ie enable_network 0)
disable_network disable_n makes a configured network unavailable for selection (ie disable_network 0)
remove_network remove_n removes a network and its configuration from the list (ie remove_network 0)
add_network add_n adds a new network to the list. Its id will be created automatically
set_network set_n shows a very short list of available options to configure a network when supplied with no parameters.
get_network get_n displays the required parameter for the specified network. See next section for a list of parameters
save_config save_c saves the configuration

2.4 手动连接流程

现在演示以下一个完整的联网过程:
0. 前提:启动wpa_supplicant

root@TinaLinux:/# wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wifi/wpa_supplicant.conf -B
Successfully initialized wpa_supplicant

1. 启动wpa_cli

root@TinaLinux:/usr/ty# wpa_cli
wpa_cli v2.6
Copyright (c) 2004-2016, Jouni Malinen  and contributors

This software may be distributed under the terms of the BSD license.
See README for more details.


Selected interface 'wlan0'

Interactive mode

2. 扫描附近的网络

> scan
OK
<3>CTRL-EVENT-SCAN-STARTED
<3>CTRL-EVENT-SCAN-RESULTS
<3>WPS-AP-AVAILABLE
<3>CTRL-EVENT-NETWORK-NOT-FOUND

3. 列出扫描到的网络

> scan_result
bssid / frequency / signal level / flags / ssid
30:23:03:d3:3f:bf       2452    -44     [WPA2-PSK-CCMP][WPS][ESS]       SJoy Games
64:09:80:7a:49:05       2427    -48     [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS]       Xiaomi_4904
30:23:03:d3:46:03       2422    -43     [WPA2-PSK-CCMP][ESS]    SJoy Games
4c:ed:fb:82:27:c0       2422    -46     [WPA2-PSK-CCMP][WPS][ESS]       AC1900-106
30:23:03:d3:3f:97       2422    -52     [WPA2-PSK-CCMP][WPS][ESS]       SJoy Games
b2:52:16:c9:06:d9       2462    -56     [WPA2-PSK-CCMP][WPS][ESS]       DIRECT-d9-HP M227f LaserJet

4. 列出当前的配置文件wpa_supplicant.conf中的配置网络
我的配置中没有任何信息

> list_network
network id / ssid / bssid / flags

5. 增加一个network
此处因为没有网络配置,所以从0开始加

> add_network 0
0

> set_network 0 ssid "APP-TEST1"  【设置SSID】
OK
> list_network    【列出当前网络】
network id / ssid / bssid / flags
0       APP-TEST1       any     [DISABLED]
> set_network 0 psk "SetPwdYourself"    【设置密码】
OK
> get_network 0 psk
*

6. 使能添加的网络

> enable_network 0
OK
<3>CTRL-EVENT-SCAN-STARTED
<3>CTRL-EVENT-SCAN-RESULTS
<3>WPS-AP-AVAILABLE

> list_network
network id / ssid / bssid / flags
0       APP-TEST1       any     [CURRENT]

7. 将刚配置的信息写入到配置文件

> save_config
OK

8. 查看配置文件信息

wpa_supplicant.conf

2.5 自动连接流程

自动连接需要提前配置wpa_supplicant.conf文件,具体配置方式见 2.1节

此处采用shell脚本的形式,避免一条条的手动输入,麻烦!!

#!/bin/sh
# Copyright (C) 2006 OpenWrt.org
#Autor: Jimmy
#Date: 2020-11-06
#Version: v1.0

start_sta() {
    echo 0 > /sys/module/bcmdhd/parameters/op_mode
    echo /lib/firmware/fw_bcm43438a0.bin > /sys/module/bcmdhd/parameters/firmware_path
    ifconfig wlan0 up
    if [[ -e "/var/run/wpa_supplicant/wlan0" ]]; then
         rm "/var/run/wpa_supplicant/wlan0"
    fi
    wpa_supplicant -i wlan0 -c /etc/wifi/wpa_supplicant.conf -B
    udhcpc -i wlan0 &
}

stop_sta() {
    killall wpa_supplicant
    killall hostapd
    killall udhcpc
    killall dnsmasq
    ifconfig wlan0 down
}

restart_sta() {
    stop_sta
    sleep 1s
    start_sta
}


start_ap() {
        if [[ -z "$1" ]]; then
                mac_addr=$(ifconfig wlan0|awk '{print $5}'|cut -c 13-|tr -d ' :\n')
                ap_name="SmartLife-$mac_addr"
                softap_up $ap_name
        else
                softap_up $1
        fi
}

stop_ap() {
        softap_down
}

restart_ap() {
        stop_ap
        sleep 1s
        start_ap $1
}

case $1 in
        start_sta)
                start_sta
                ;;
        stop_sta)
                stop_sta
                ;;
        restart_sta)
                restart_sta
                ;;

        start_ap)
                start_ap $2
                ;;
        stop_ap)
                stop_ap
                ;;
        restart_ap)
                restart_ap $2
                ;;
        *)
                echo "Input option: $1 was error"
                ;;
esac

3. udhcpc

DHCP (Dynamic Host Configuration Protocol) ,目的就是方便管理区域网络內的裝置,当一台 PC 连接到区域网络时,可向 DHCP Server 自动获取可用的 IP,并取得 subnet mask,gateway。

既然是网络通信,肯定是要用DHCP的,网络通信方式一般包括如下三种:

    1. 接入已有网络:即作为STA方式,此时DHCP为客户端
    1. 做AP热点:即AP模式,DHCP此时为服务端
    1. 固定IP:DHCP不工作

DHCP的参数如下:

root@TinaLinux:/usr/ty# udhcpc -h
udhcpc: option requires an argument: h
BusyBox v1.24.1 () multi-call binary.

Usage: udhcpc [-fbqRB] [-t N] [-T SEC] [-A SEC/-n]
        [-i IFACE] [-s PROG] [-p PIDFILE]
        [-oC] [-r IP] [-V VENDOR] [-F NAME] [-x OPT:VAL]... [-O OPT]...

        -i,--interface IFACE    Interface to use (default eth0)
        -s,--script PROG        Run PROG at DHCP events (default /usr/share/udhcpc/default.script)
        -p,--pidfile FILE       Create pidfile
        -B,--broadcast          Request broadcast replies
        -t,--retries N          Send up to N discover packets (default 3)
        -T,--timeout SEC        Pause between packets (default 3)
        -A,--tryagain SEC       Wait if lease is not obtained (default 20)
        -n,--now                Exit if lease is not obtained
        -q,--quit               Exit after obtaining lease
        -R,--release            Release IP on exit
        -f,--foreground         Run in foreground
        -b,--background         Background if lease is not obtained
        -S,--syslog             Log to syslog too
        -r,--request IP         Request this IP address
        -o,--no-default-options Don't request any options (unless -O is given)
        -O,--request-option OPT Request option OPT from server (cumulative)
        -x OPT:VAL              Include option OPT in sent packets (cumulative)
                                Examples of string, numeric, and hex byte opts:
                                -x hostname:bbox - option 12
                                -x lease:3600 - option 51 (lease time)
                                -x 0x3d:0100BEEFC0FFEE - option 61 (client id)
        -F,--fqdn NAME          Ask server to update DNS mapping for NAME
        -V,--vendorclass VENDOR Vendor identifier (default 'udhcp VERSION')
        -C,--clientid-none      Don't send MAC as client identifier
Signals:
        USR1    Renew lease
        USR2    Release lease

你可能感兴趣的:(【全志系统 - WIFI - 4】- STA模式)