美文网首页
arm 使用wpa_cli连接wifi

arm 使用wpa_cli连接wifi

作者: 坤kln | 来源:发表于2019-05-29 12:41 被阅读0次

    公司使用的是IMX6的ARM板,板子已经带了8192cu的驱动,使用 lsmod 命令可以查看已经安装的mod

    使用的USB为:树莓派 Raspberry Pi 无线USB网卡 EDUP EP-N8508GS 黄金版 免驱

    lsmod
    结果:
    Module                  Size  Used by
    8192cu                497965  0
    

    驱动文件在 /lib/modules/8192cu.ko

    接上USBwifi后,先查看能否看到无线网卡,使用ifconfig命令或者iwconfig命令,我这边显示的名称为wlan0

    wlan0     Link encap:Ethernet  HWaddr e8:4e:06:6f:87:dc  
              inet addr:192.168.1.103  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::ea4e:6ff:fe6f:87dc/64 Scope:Link
              UP BROADCAST MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:64 overruns:0 frame:0
              TX packets:0 errors:0 dropped:2 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:2870410 (2.7 MiB)  TX bytes:13714 (13.3 KiB)
    
    # 启用网卡
    ifconfig wlan0 up
    # 禁用网卡
    ifconfig wlan0 down
    

    使用 iwlist wlan0 scan可以使用无线网卡wlan0扫描可见的wifi,会显示很多,使用 iwlist wlan0 scan | grep SSID只显示名称,不过一般使用wpa_cli命令搜索wifi

    下面先说一下几个配置文件:

    • /etc/resolv.conf 域名解析的地址配置文件,通过命令udhcpc -iwlan0可以从DHCP服务器上获取IP地址和有效的DNS并自动写入这个文件
    • /etc/network/interfaces 网卡配置文件,下面是arm板上的interfaces文件内容:
    # /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)
    
    # The loopback interface
    auto lo
    iface lo inet loopback
    
    # Wireless interfaces
    auto wlan0
    iface wlan0 inet dhcp
    #       address 192.168.1.62
    #       netmask 255.255.255.0
    #       gateway 192.168.1.1
    #       wireless_mode managed
    #       wireless_essid any
    #       wpa-driver wext
    #       wpa-conf /etc/wpa_supplicant.conf
    
    #iface atml0 inet dhcp
    
    # Wired or wireless interfaces
    auto eth0
    #iface eth0 inet dhcp
    iface eth0 inet static
    #       address 192.168.1.62
            address 192.168.100.20
            netmask 255.255.255.0
    #       gateway 192.168.1.1
            gateway 192.168.100.10
    
    # Ethernet/RNDIS gadget (g_ether)
    # ... or on host side, usbnet and random hwaddr
    #iface usb0 inet static
    #       address 192.168.7.2
    #       netmask 255.255.255.0
    #       network 192.168.7.0
    #       gateway 192.168.7.1
    
    # Bluetooth networking
    #iface bnep0 inet dhcp
    
    • /etc/wpa_supplicant.conf 无线网卡连接配置文件,下面是wpa_supplicant.conf配置文件内容:
    ctrl_interface=/var/run/wpa_supplicant
    # 如果要使用wpa_cli -i wlan0 save_config保存网络配置,那么update_config要设置为0
    # 如果update_config设置为0,保存会失败,但是还没有提示为什么
    update_config=1
    device_type=0-00000000-0
    
    # wifi配置例子
    network={
            ssid="TP-LINK"
            psk="12345678"
            key_mgmt=WPA-PSK
    }
    

    完整的wpa_supplicant.conf配置说明可以看官方配置文件说明:http://w1.fi/cgit/hostap/plain/wpa_supplicant/wpa_supplicant.conf

    wpa_supplicant

    通过pgrep -af wpa_supplicant查看服务是否启动

    如果没有自动启动,可以自己手动启动,命令如下,如果配置问价有问题,会启动失败的

    /usr/sbin/wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf -Dwext
    

    开始连接wifi

    使用wpa_cli命令直接进入wpa的客户端命令行模式,显示如下:

    wpa_cli v0.8.x_rtw_r7475.20130812_beta
    Copyright (c) 2004-2011, Jouni Malinen <j@w1.fi> and contributors
    
    This program is free software. You can distribute it and/or modify it
    under the terms of the GNU General Public License version 2.
    
    Alternatively, this software may be distributed under the terms of the
    BSD license. See README and COPYING for more details.
    
    Selected interface 'wlan0'
    
    Interactive mode
    >
    

    不过我不喜欢这种模式,特别是输入命令不能修改,一般我使用wpa_cli直接加指令

    # 查看命令帮助
    wpa_cli help
    # 搜索可见wifi
    wpa_cli -i wlan0 scan
    # 获取搜索结果
    wpa_cli -i wlan0 scan_result
    # 获取已经配置的wifi设置
    wpa_cli -i wlan0 list_networks
    # 查看当前wifi状态
    wpa_cli -i wlan0 status
    # 新建wifi配置,会返回一个新的网络整数ID,比如:0
    wpa_cli -i wlan0 add_network
    # 假设新wifi的网络ID为0
    # 设置wifi的ssid
    # ssid需要添加双引号,双引号还要加斜杠或者单引号
    wpa_cli -i wlan0 set_network 0 ssid '"TP-LINK"'
    wpa_cli -i wlan0 set_network 0 ssid \"TP-LINK\"
    # 设置加密方式,WPA-PSK代表:WPA-PSK/WPA2-PSK
    wpa_cli -i wlan0 set_network 0 key_mgmt WPA-PSK
    # 设置wifi密码,和账号一样需要双引号
    wpa_cli -i wlan0 set_network 0 psk '"12345678"'
    wpa_cli -i wlan0 set_network 0 psk \"12345678\"
    # 新配置的wifi默认是禁用的,启用,启用后如果没有连接wifi,新增wifi可见就会自动连接的
    wpa_cli -i wlan0 enable_network 0
    # 如果有多个wifi,可以通过如下命令选择wifi
    wpa_cli -i wlan0 select_network 0
    # 再次查看状态,查看是否已经连接网络
    # wpa_state=COMPLETED表示完成连接,但是如果signal_level比较小时表示实际没有连接
    wpa_cli -i wlan0 status
    # 保存wifi配置,最好在启用wifi后再保存,否则保存的wifi是默认禁用的
    wpa_cli -i wlan0 save_config
    

    按如上指令一般来说是可以正常连接的

    下面是其他常用命令

    # 重新加载配置文件
    wpa_cli -i wlan0 reconfigure
    # 断开wifi连接
    wpa_cli -i wlan0 disconnect
    # 重新连接
    wpa_cli -i wlan0 reconnect
    # 移除wifi配置
    wpa_cli -i wlan0 remove_network 0
    # 终止wpa_supplicant后台服务程序
    wpa_cli -i wlan0 terminate
    # 也可以使用下面指令终止wpa_supplicant 
    killall wpa_supplicant > /dev/null 2>&1
    # 如果终止wpa_supplicant会造成使用的无线网卡被禁用,使用ifconfig wlan0 up重新启动网卡
    

    有时会出现网络没有自动从DHCP中获取IP地址的,可以自己手动获取:

    udhcpc -iwlan0
    

    wpa_cli -i wlan0 status命令显示的状态wpa_state变量有如下几种值:

    enum WPA_States {
            /**
             * WPA_DISCONNECTED - Disconnected state
             *
             * This state indicates that client is not associated, but is likely to
             * start looking for an access point. This state is entered when a
             * connection is lost.
             */
            WPA_DISCONNECTED = 0,
    
            /**
             * WPA_INTERFACE_DISABLED - Interface disabled
             *
             * This state is entered if the network interface is disabled, e.g.,
             * due to rfkill. wpa_supplicant refuses any new operations that would
             * use the radio until the interface has been enabled.
             */
            WPA_INTERFACE_DISABLED,
    
            /**
             * WPA_INACTIVE - Inactive state (wpa_supplicant disabled)
             *
             * This state is entered if there are no enabled networks in the
             * configuration. wpa_supplicant is not trying to associate with a new
             * network and external interaction (e.g., ctrl_iface call to add or
             * enable a network) is needed to start association.
             */
            WPA_INACTIVE,
    
            /**
             * WPA_SCANNING - Scanning for a network
             *
             * This state is entered when wpa_supplicant starts scanning for a
             * network.
             */
            WPA_SCANNING,
    
            /**
             * WPA_AUTHENTICATING - Trying to authenticate with a BSS/SSID
             *
             * This state is entered when wpa_supplicant has found a suitable BSS
             * to authenticate with and the driver is configured to try to
             * authenticate with this BSS. This state is used only with drivers
             * that use wpa_supplicant as the SME.
             */
            WPA_AUTHENTICATING,
    
            /**
             * WPA_ASSOCIATING - Trying to associate with a BSS/SSID
             *
             * This state is entered when wpa_supplicant has found a suitable BSS
             * to associate with and the driver is configured to try to associate
             * with this BSS in ap_scan=1 mode. When using ap_scan=2 mode, this
             * state is entered when the driver is configured to try to associate
             * with a network using the configured SSID and security policy.
             */
            WPA_ASSOCIATING,
    
            /**
             * WPA_ASSOCIATED - Association completed
             *
             * This state is entered when the driver reports that association has
             * been successfully completed with an AP. If IEEE 802.1X is used
             * (with or without WPA/WPA2), wpa_supplicant remains in this state
             * until the IEEE 802.1X/EAPOL authentication has been completed.
             */
            WPA_ASSOCIATED,
    
            /**
             * WPA_4WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress
             *
             * This state is entered when WPA/WPA2 4-Way Handshake is started. In
             * case of WPA-PSK, this happens when receiving the first EAPOL-Key
             * frame after association. In case of WPA-EAP, this state is entered
             * when the IEEE 802.1X/EAPOL authentication has been completed.
             */
            WPA_4WAY_HANDSHAKE,
    
            /**
             * WPA_GROUP_HANDSHAKE - WPA Group Key Handshake in progress
             *
             * This state is entered when 4-Way Key Handshake has been completed
             * (i.e., when the supplicant sends out message 4/4) and when Group
             * Key rekeying is started by the AP (i.e., when supplicant receives
             * message 1/2).
             */
            WPA_GROUP_HANDSHAKE,
    
            /**
             * WPA_COMPLETED - All authentication completed
             *
             * This state is entered when the full authentication process is
             * completed. In case of WPA2, this happens when the 4-Way Handshake is
             * successfully completed. With WPA, this state is entered after the
             * Group Key Handshake; with IEEE 802.1X (non-WPA) connection is
             * completed after dynamic keys are received (or if not used, after
             * the EAP authentication has been completed). With static WEP keys and
             * plaintext connections, this state is entered when an association
             * has been completed.
             *
             * This state indicates that the supplicant has completed its
             * processing for the association phase and that data connection is
             * fully configured.
             */
            WPA_COMPLETED
        };
    

    下面是wpa_cli的命令帮助

    commands:
      status [verbose] = get current WPA/EAPOL/EAP status
      ping = pings wpa_supplicant
      relog = re-open log-file (allow rolling logs)
      note <text> = add a note to wpa_supplicant debug log
      mib = get MIB variables (dot1x, dot11)
      help = show this usage help
      interface [ifname] = show interfaces/select interface
      level <debug level> = change debug level
      license = show full wpa_cli license
      quit = exit wpa_cli
      set = set variables (shows list of variables when run without arguments)
      get <name> = get information
      logon = IEEE 802.1X EAPOL state machine logon
      logoff = IEEE 802.1X EAPOL state machine logoff
      pmksa = show PMKSA cache
      reassociate = force reassociation
      preauthenticate <BSSID> = force preauthentication
      identity <network id> <identity> = configure identity for an SSID
      password <network id> <password> = configure password for an SSID
      new_password <network id> <password> = change password for an SSID
      pin <network id> <pin> = configure pin for an SSID
      otp <network id> <password> = configure one-time-password for an SSID
      passphrase <network id> <passphrase> = configure private key passphrase for an SSID
      bssid <network id> <BSSID> = set preferred BSSID for an SSID
      list_networks = list configured networks
      select_network <network id> = select a network (disable others)
      enable_network <network id> = enable a network
      disable_network <network id> = disable a network
      add_network = add a network
      remove_network <network id> = remove a network
      set_network <network id> <variable> <value> = set network variables (shows list of variables when run without arguments)
      get_network <network id> <variable> = get network variables
      save_config = save the current configuration
      disconnect = disconnect and wait for reassociate/reconnect command before connecting
      reconnect = like reassociate, but only takes effect if already disconnected
      scan = request new BSS scan
      scan_results = get latest scan results
      bss <<idx> | <bssid>> = get detailed scan result info
      get_capability <eap/pairwise/group/key_mgmt/proto/auth_alg> = get capabilies
      reconfigure = force wpa_supplicant to re-read its configuration file
      terminate = terminate wpa_supplicant
      interface_add <ifname> <confname> <driver> <ctrl_interface> <driver_param> <bridge_name> = adds new interface, all parameters but <ifname> are optional
      interface_remove <ifname> = removes the interface
      interface_list = list available interfaces
      ap_scan <value> = set ap_scan parameter
      scan_interval <value> = set scan_interval parameter (in seconds)
      bss_expire_age <value> = set BSS expiration age parameter
      bss_expire_count <value> = set BSS expiration scan count parameter
      stkstart <addr> = request STK negotiation with <addr>
      ft_ds <addr> = request over-the-DS FT with <addr>
      wps_pbc [BSSID] = start Wi-Fi Protected Setup: Push Button Configuration
      wps_pin <BSSID> [PIN] = start WPS PIN method (returns PIN, if not hardcoded)
      wps_check_pin <PIN> = verify PIN checksum
      wps_cancel Cancels the pending WPS operation
      wps_reg <BSSID> <AP PIN> = start WPS Registrar to configure an AP
      wps_ap_pin [params..] = enable/disable AP PIN
      wps_er_start [IP address] = start Wi-Fi Protected Setup External Registrar
      wps_er_stop = stop Wi-Fi Protected Setup External Registrar
      wps_er_pin <UUID> <PIN> = add an Enrollee PIN to External Registrar
      wps_er_pbc <UUID> = accept an Enrollee PBC using External Registrar
      wps_er_learn <UUID> <PIN> = learn AP configuration
      wps_er_set_config <UUID> <network id> = set AP configuration for enrolling
      wps_er_config <UUID> <PIN> <SSID> <auth> <encr> <key> = configure AP
      ibss_rsn <addr> = request RSN authentication with <addr> in IBSS
      suspend = notification of suspend/hibernate
      resume = notification of resume/thaw
      drop_sa = drop SA without deauth/disassoc (test command)
      roam <addr> = roam to the specified BSS
      sta_autoconnect <0/1> = disable/enable automatic reconnection
      tdls_discover <addr> = request TDLS discovery with <addr>
      tdls_setup <addr> = request TDLS setup with <addr>
      tdls_teardown <addr> = tear down TDLS with <addr>
      signal_poll = get signal parameters
    

    参考:

    http://www.forlinx.com/zixun/49.htm

    https://blog.csdn.net/jack_a8/article/details/43062895

    https://www.cnblogs.com/little-ant/p/3730148.html

    http://shumeipai.nxez.com/2013/09/30/use-wpa-cli-command-line-to-configure-wi-fi-wireless-lan.html

    https://segmentfault.com/a/1190000011579147

    http://w1.fi/

    相关文章

      网友评论

          本文标题:arm 使用wpa_cli连接wifi

          本文链接:https://www.haomeiwen.com/subject/nonetctx.html