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 <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/

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,053评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,527评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,779评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,685评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,699评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,609评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,989评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,654评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,890评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,634评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,716评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,394评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,976评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,950评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,191评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,849评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,458评论 2 342

推荐阅读更多精彩内容