arm 使用wpa_cli連接wifi

公司使用的是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 <[email protected]> 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/

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章