Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iw命令用法 #65

Open
cisen opened this issue Nov 22, 2018 · 3 comments
Open

iw命令用法 #65

cisen opened this issue Nov 22, 2018 · 3 comments
Labels

Comments

@cisen
Copy link
Owner

cisen commented Nov 22, 2018

最新的raspbian已经有了wifi必要的包,直接插上就可以用了。不过最好还是可以看看 iwconfig 确认一下,输入 iwconfig 显示如下:

pi@raspberrypi:~$ iwconfig  
wlan0     unassociated  Nickname:""
          Mode:Managed  Frequency=2.412 GHz  Access Point: Not-Associated   
          Sensitivity:0/0  
          Retry:off   RTS thr:off   Fragment thr:off
          Power Management:off
          Link Quality:0  Signal level:0  Noise level:0
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0

lo        no wireless extensions.

eth0      no wireless extensions.
```shell
如果出现了wlan0,那说明网卡已经正常工作了。(这里的示例是已经用usb无线网卡连接上网络了,所以会显示ESSID。)如果这里的显示不正常,请安装连接wifi必要的包
## 连接过程
以连接"liangym"为例,我们看看具体的过程:
(1) 设置要连接的网络类型(经确认,此步骤可忽略):
```shell
iwpriv ra0 set NetworkType=Infra

(2) 设置要连接的无线网络的安全模式(仅支持WPA Version 1需要使用AuthMode=WPAPSK):

iwpriv ra0 set AuthMode=WPA2PSK

(3) 设置网络加密方式:(TKIP即为TKIP)

iwpriv ra0 set EncrypType=AES

(4) 设置连接时的密码: ********为加密网络的密码

iwpriv ra0 set WPAPSK=*******

(5) 连接该网络:有两种方式:

    <1> 用iwpriv命令

iwpriv ra0 set SSID=liangym

    <2> 用iwconfig命令

iwconfig ra0 essid liangym

至此,如果密码正确,就可以连接上网络liangym了。如果你不放心,可以用命令查看状态:

iwpriv ra0 connStatus

当然,你也可以用iwconfig命令查看状态,这里不再赘述。

命令

sudo apt-get install wireless-tools wpasupplicant firmware-realtek

使用iwlist scan[ning]的时候,是需要超级用户的权限的,所以去尝试用sudo执行
1、iwlist 命令:用于对/proc/net/wireless文件进行分析,得出无线网卡相关信息

# iwlist wlan0 scanning 搜索当前无线网络
# iwlist wlan0 frequen  显示频道信息
# iwlist wlan0 rate  显示连接速度
# iwlist wlan0 power  显示电源模式
# iwlist wlan0 txpower 显示功耗
# iwlist wlan0 retry  显示重试连接次数(网络不稳定查看)
# iwlist wlan0 ap 显示热点信息
# iwlist --help 显示帮助信息
# iwlist --version 显示版本信息

2、iwconfig  系统配置无线网络设备或显示无线网络设备信息。iwconfig 命令类似于ifconfig命令,但是他配置对象是无线网卡,它对网络设备进行无线操作,如设置无线通信频段

auto 自动模式
essid 设置ESSID
nwid 设置网络ID
freq 设置无线网络通信频段
chanel 设置无线网络通信频段
sens 设置无线网络设备的感知阀值
mode 设置无线网络设备的通信设备
ap 强迫无线网卡向给定地址的接入点注册
nick<名字> 为网卡设定别名
rate<速率> 设定无线网卡的速率
rts<阀值> 在传输数据包之前增加一次握手,确信信道在正常的
power 无线网卡的功率设置

3、iw 是一种新的基于 nl80211 的用于无线设备的CLI配置实用程序。它支持最近已添加到内核所有新的驱动程序。采用无线扩展接口的旧工具iwconfig已被废弃,强烈建议切换到 iw 和 nl80211。
Linux内核的其余部分,iw 仍处于开发阶段。功能被随时添加。 iw 的唯一文档就是此页和“iw help”的输出。 请帮助扩大这个页面。
有一个页面列出iwconfig和iw的用例:替换 iwconfig.

# iw help    # 帮助
# iw list    # 获得所有设备的功能,如带宽信息(2.4GHz,和5GHz),和802.11n的信息
# iw dev wlan0 scan    # 扫描
# iw event    # 监听事件
# iw dev wlan0 link    # 获得链路状态
# iw wlan0 connect foo    # 连接到已禁用加密的AP,这里它的SSID是foo
# iw wlan0 connect foo 2432  # 假设你有两个AP SSID 都是 foo ,你知道你要连接的是在 2432 频道
# iw wlan0 connect foo keys 0:abcde d:1:0011223344    # 连接到使用WEP的AP
# iw dev wlan1 station dump    # 获取station 的统计信息
# iw dev wlan1 station get     # 获得station对应的peer统计信息
# iw wlan0 set bitrates legacy-2.4 12 18 24    # 修改传输比特率
# iw dev wlan0 set bitrates mcs-5 4    # 修改tx HT MCS的比特率
# iw dev wlan0 set bitrates mcs-2.4 10
# iw dev wlan0 set bitrates mcs-5    # 清除所有 tx 比特率和设置的东西来恢复正常
# iw dev  set txpower  []   #设置传输功率
# iw phy  set txpower  []   #设置传输功率
# iw dev wlan0 set power_save on  #设置省电模式
# iw dev wlan0 get power_save  #查询当前的节电设定
# iw phy phy0 interface add moni0 type monitor  #添加一个 monitor 接口
@cisen
Copy link
Owner Author

cisen commented Nov 24, 2018

https://blog.csdn.net/robertsong2004/article/details/40044947

官方译文

关于 iw
iw 是一种新的基于 nl80211 的用于无线设备的CLI配置实用程序。它支持最近已添加到内核所有新的驱动程序。采用无线扩展接口的旧工具iwconfig已被废弃,强烈建议切换到 iw 和 nl80211。

像Linux内核的其余部分,iw 仍处于开发阶段。功能被随时添加。 iw 的唯一文档就是此页和“iw help”的输出。 请帮助扩大这个页面。

有一个页面列出iwconfig和iw的用例:替换 iwconfig。

获得 iw

iw发布压缩包可以从这里获取: http://kernel.org/pub/software/network/iw/.

另外,你也可以从 git 下载 iw: http://git.kernel.org/?p=linux/kernel/git/jberg/iw.git.

编译要求
libnl >= libnl1

libnl-dev >= libnl-dev-1

pkg-config
为了使用 iw 你需要有libnl,第一个工作版本是1.0 pre8,因为此版本中引入了 genl, Generic Netlink,它是nl80211所依赖的。如果你的发行版的libnl是一个错误的版本,那么你就必须下载并自己编译libnl(http://www.infradead.org/~tgr/libnl/)。

帮助
只要在命令行输入

iw help
它会打印出它所支持的命令。

获取设备的功能
使用以下方法来获得所有设备的功能,如带宽信息(2.4GHz,和5GHz),和802.11n的信息:

iw list
扫描

iw dev wlan0 scan
监听事件
只要使用

iw event
调试时观察auth/assoc/deauth/disassoc帧可能有帮助,使用

iw event -f
有时时间信息也是有用的:

iw event -t
获得链路状态
为了确定您是否连接到一个AP上,如果你是最后一个使用的发送速率,您可以使用下面的命令。

关联到传统(非802.11n标准)的AP的输出示例:

iw dev wlan0 link
Connected to 04:21:b0:e8:c8:8b (on wlan0)
SSID: attwifi
freq: 2437
RX: 2272 bytes (18 packets)
TX: 232 bytes (3 packets)
signal: -57 dBm
tx bitrate: 36.0 MBit/s
关联到802.11n的AP的输出示例:

iw dev wlan0 link
Connected to 68:7f:74:3b:b0:01 (on wlan0)
SSID: tesla-5g-bcm
freq: 5745
RX: 30206 bytes (201 packets)
TX: 4084 bytes (23 packets)
signal: -31 dBm
tx bitrate: 300.0 MBit/s MCS 15 40Mhz short GI
当输出没有连接到一个AP的例子:

iw dev wlan0 link
Not connected.
这会发生在当你没有连接到一个AP的情况下。 要连接到一个AP可以使用 iw connect ,如果连接要求:

无加密
使用WEP加密
如果你需要连接到一个AP要求使用WPA或WPA2,那么你必须使用wpa_supplicant 。

建立基本连接
你可以使用iw直接连接到一个AP上,当且仅当AP:

无加密
使用WEP加密
然而应该指出的是,如果你断开AP的连接时,这可以在一个繁忙的环境中频繁发生,你将需要重新发出命令。 如果你不想这样做,你可以使用wpa_supplicant,它会在你断开连接后自动尝试重新连接。

如果您选择自己处理断开连接,你可以使用 iw connect 如下所示。

如要连接到已禁用加密的AP,这里它的SSID是foo :

iw wlan0 connect foo
假设你有两个AP SSID 都是 foo ,你知道你要连接的是在 2432 频道,你可以指定使用的频率:

iw wlan0 connect foo 2432
要连接到使用WEP的AP,则可以使用:

iw wlan0 connect foo keys 0:abcde d:1:0011223344
获取station统计数据
要获取station 的统计信息,如发送/接收的字节,最后发送的比特率(包括MCS率),你可以执行下面的命令:

$ iw dev wlan1 station dump
Station 12:34:56:78:9a:bc (on wlan0)
inactive time: 304 ms
rx bytes: 18816
rx packets: 75
tx bytes: 5386
tx packets: 21
signal: -29 dBm
tx bitrate: 54.0 MBit/s
获得station对应的peer统计信息
如果你想获得 station 对应的 peer的统计信息,你可以使用下面的命令:

sudo iw dev wlan1 station get
在STA的情况下,上述的将是你的AP的MAC地址。

修改传输比特率
iw 支持修改发送的比特率,假托传统和 HT MCS速率。 这是通过设定允许的比特率掩码来实现,你也可以清除该掩码。

修改TX传统的比特率
您可以设定用于使用某些传统的比特率传输的性能。 例如:

iw wlan0 set bitrates legacy-2.4 12 18 24
以下是如何使能一些人所说的“Purge G”来禁用802.11b 关联:

iw wlan0 set bitrates legacy-2.4 6 12 24
修改tx HT MCS的比特率
设置使用MCS率传输的能力是通过让你指定频段和MCS率来实现。 注意,是否该设备实际上监听你的请求将根据设备驱动程序和固件的配合而变化。 例如:

iw dev wlan0 set bitrates mcs-5 4

iw dev wlan0 set bitrates mcs-2.4 10
清除所有 tx 比特率和设置的东西来恢复正常:

iw dev wlan0 set bitrates mcs-2.4
iw dev wlan0 set bitrates mcs-5
设置传输功率
可以通过使用各自的phy的任一设备接口名称来设置 txpower 。

iw dev set txpower <auto|fixed|limit> []
iw phy set txpower <auto|fixed|limit> []
省电
为在默认情况下使能 power save,你可以使用:

sudo iw dev wlan0 set power_save on
对于mac80211驱动程序,这意味着动态节电模式被启用。

查询当前的节电设定,您可以使用:

iw dev wlan0 get power_save
使用iw添加接口
有几种模式可以支持。 支持的模式包括:

monitor
managed [also station]
wds
mesh [also mp]
ibss [also adhoc]
要查看这些说明,请阅读我们的模式文档 。

例如,要添加一个 monitor 接口:

iw phy phy0 interface add moni0 type monitor
where you can replace monitor by anything else and moni0 by the interface name, and need to replacephy0 by the PHY name for your hardware (usually phy0 will be correct unless you hotplugged or reloaded any modules.) If your udev is configured incorrectly, the newly created virtual interface may be renamed by it right away, use ip link to list all interfaces.

Note that in case you want to monitor 802.11n you will need to specify channel width (20 or 20/40MHz) and in case of 20/40MHz if the upper or lower channel is being used. To do so you would use:

iw dev set freq [HT20|HT40+|HT40-]
or

iw phy set freq [HT20|HT40+|HT40-]
You can also specify channel instead of frequency:

iw phy set channel [HT20|HT40+|HT40-]
iw dev set channel [HT20|HT40+|HT40-]
To create a new managed mode interface you would use:

iw phy phy0 interface add wlan10 type managed
Note that the interface is automatically put into AP mode when using hostapd.

Modifying monitor interface flags
You can customize the type of monitor interface you create. This can be very useful for debugging purposes on end user systems. For example suppose you want to help a user you can take advantage of the fact that a monitor interface in mac80211 uses radiotap to pass up to userspace additional data. Say we want to help a user fish out data without affecting the device's performance by setting it it to a full monitor interface an monitor interface with no additional monitor flags can be created as follows:

iw dev wlan0 interface add fish0 type monitor flags none
You can then request the user to use tcpdump on a session:

tcpdump -i fish0 -s 65000 -p -U -w /tmp/fishing.dump
The nice thing about these type of alternative monitor interfaces is you can further extend radiotap even with vendor extensions to add more data to radiotap to help debug device specific features.

Keep in mind this requires drivers to honor mac80211's flag requests strictly, so drivers like ath5k and ath9k which still enable flags based on operation mode need to be fixed to take advantage of this.

Monitor flags possible
The following are flags you can specify:

none
fcsfail
plcpfail
control
otherbss
cook
Deleting interfaces with iw
The command line is:

iw dev moni0 del
Where "moni0" was the virtual interface interface that was created with the first command

Virtual vif support
There is a dedicated section for virtual vif support, see the iw vif page.

Setting frequency with iw
The command line is:

iw dev wlan0 set freq 2412 [HT20|HT40+|HT40-]
Setting channel with iw
The command line is:

iw dev wlan0 set channel 1 [HT20|HT40+|HT40-]
Updating your regulatory domain
The command line is:

iw reg set alpha2
Where "alpha2" is the ISO/IEC 3166 alpha2 country code. The information used and set comes from our regulatory infrastructure.

You can also use the latest wpa_supplicant (as of 0.6.7) now to change your regulatory domain, to do so just add a "COUNTRY=US" entry into your configuration for example.

Creating and inspecting Mesh Point interfaces with iw
You may add a mesh interface to drivers that support Mesh Point operation. Mesh Point interfaces have a mesh_id parameter which may be up to 32 bytes long. For example, to add an interface "mesh0" to device phy0 with mesh_id "mymesh",

iw phy phy0 interface add mesh0 type mp mesh_id mymesh
Mesh Point interfaces, by default, are configured on Channel 1. Mesh Point operation begins when the interface is brought up. In the default configuration, Mesh Point interfaces will automatically detect and attempt to create Peer Links with other Mesh Points (peers) having the same mesh ID. Use the station list and station statistics to see the peer list and Peer Link status.

After sending traffic (ex: pinging another mesh node), you may wish to see a list of Mesh Paths:

iw dev mesh0 mpath dump
Please see the open80211s.org HOWTO for further details on Mesh Point related commands and their output, as well as more examples. iw also provides commands for advanced Mesh Point configuration. These are documented in the Advanced Tinkering section of the open80211s HOWTO.

Setting up a WDS peer
WDS mode is a non-standard extension to the IEEE 802.11 standard to allow transparent Ethernet bridging on the station and to implement seamingless hand-over for wireless clients roaming between different access points. Due to its non-standard nature, WDS is often implemented differently in wireless drivers and vendor firmwares making them incompatible with each other. In order to use WDS, one should use the same hardware and software on all deployed wireless devices to maintain compatibility.

To create a WDS peer you will first need to create an interface of WDS type, and then set the peer:

iw phy phy0 interface add wds0 type wds
iw dev wds0 set peer
In order for this to work the driver must implement the cfg80211 callback set_wds_peer(). mac80211 implements this callback, so the respective mac80211 driver would just need to support WDS type interfaces. What WDS will do is replace the first address on the 802.11 header with the peer address when TXing frames. Instead of using WDS though you may want to consider using 4-address mode described below if you have control over the software running on the AP and respective clients/peers connected.

Using 4-address for AP and client mode
In some situations it might be useful to run a network with an Access Point and multiple clients, but with each client bridged to a network behind it. For this to work, both the client and the AP need to transmit 4-address frames, containing both source and destination MAC addresses. 4-address mode is howOpenWrt supports WDS mode for mac80211 drivers, that is if you enable wds option on your OpenWrt OpenWrt wireless configuration you will end up using 4-address mode. 4-address mode is not compatible with other WDS implementations, ie, you'll need all endpoints using this mode in order for WDS to work appropriately.

Linux wireless has support for 4-address mode for AP and STAs but each driver needs to define this capability explicitly. All mac80211 drivers support 4-address mode if AP or STA modes of operation are supported respectively.

On the AP side you can enable 4-address frames for individual clients by isolating them in separate AP VLANs which are configured in 4-address mode. Such an AP VLAN will be limited to one client only, and this client will be used as the destination for all traffic on its interface, regardless of the destination MAC address in the packet headers. The advantage of this mode compared to regular WDS mode is that it's easier to configure and does not require a static list of peer MAC addresses on any side. 4-address mode is incompatible with WDS.

To enable 4-address mode when creating an interface you should add 4addr on, for example:

iw phy phy0 interface add moni0 type managed 4addr on
When the client side interface is included in a bridge, add -b <bridge_interface> when runningwpa_supplicant.

In hostapd you can enable this with the flag on hostapd.conf:

wds_sta=1
Please note 4-address mode is currently broken on 3.9 because of commit 576eb62598f10c8c7fd75703fe89010cdcfff596 , this topic is currently being addressed on the mailing lists for a resolution.

Creating packet coalesce rules
In most cases, host that receives IPv4 and IPv6 multicast/broadcast packets does not do anything with these packets. Therefore the reception of these unwanted packets causes unnecessary processing and power consumption.

Packet coalesce feature helps to reduce number of receive interrupts to host by buffering these packets in firmware/hardware for some predefined time. Receive interrupt will be generated when one of the following events occur.

Expiration of hardware timer whose expiration time is set to maximum coalescing delay of matching coalesce rule.
Coalescing buffer in hardware reaches it's limit.
Packet doesn't match any of the configured coalesce rules.
To view coalesce configuration support information, you can use 'iw phy0 info'. Here is an example output:

Coalesce support:
* Maximum 8 coalesce rules supported
* Each rule contains upto 4 patterns of 1-4 bytes,
maximum packet offset 50 bytes
* Maximum supported coalescing delay 100 msecs
You need to configure following parameters for creating a coalesce rule.

Maximum coalescing delay
List of packet patterns which needs to be matched
Condition for coalescence. pattern 'match' or 'no match'
Multiple such rules can be provided through a configuration file.

To enable coalesce feature using rules listed in coalesce.conf file, you can use:

iw phy phy0 enable coalesce.conf
Where coalesce.conf contains:

delay=25
condition=0
patterns=8+34:xx:ad:22,10+23:45:67,59:33:xx:25,ff:ff:ff:ff
delay=40
condition=1
patterns=12+00:xx:12,23:45:67,46:61:xx:50
To display current coalesce configuration, you can use:

$ iw phy phy0 coalesce show
Coalesce is enabled:
Rule - max coalescing delay: 25msec condition:match

  • packet offset: 8 pattern: 34:--:ad:22
  • packet offset: 10 pattern: 23:45:67
  • packet offset: 0 pattern: 59:33:--:25
  • packet offset: 0 pattern: ff:ff:ff:ff
    Rule - max coalescing delay: 40msec condition:not match
  • packet offset: 12 pattern: 00:--:12
  • packet offset: 0 pattern: 23:45:67
  • packet offset: 0 pattern: 46:61:--:50
    To disable coalesce feature, you can use:

iw phy phy0 coalesce disable
'iw display' output when coalesce is not configured:

$ iw phy phy0 coalesce show
Coalesce is disabled.

@cisen
Copy link
Owner Author

cisen commented Nov 24, 2018

https://blog.csdn.net/DXCyber409/article/details/80574111
iw命令
man手册中可以找到说明 iw - show / manipulate wireless devices and their configuration

这意味着iw既可以查看信息,又可以管理无线网络设备,还能更改配置,属于比较全能的命令。

缺点:实测对USB网卡支持性很差,几乎只适用于物理接入设备(PCI板载设备)。

常用命令列表:   

iw list # 查看本机支持的无线特性,such as band information (2.4 GHz, and 5 GHz), and 802.11n information
iw dev wlan0 scan # 扫描无线网络,列表的内容都是实时更新的
iw dev wlan0 link # 获取设备连接状态信息(实测不包含IP地址)
iw wlan0 info # 获取设备工作状态信息
iw event # 获取所有网络设备的工作日志信息
英文原文介绍:https://wireless.wiki.kernel.org/en/users/Documentation/iw

译文:https://blog.csdn.net/robertsong2004/article/details/40044947

ifconfig命令
ifconfig - configure a network interface 配置网络接口命令

通用的接口操作命令,但通用意味着只能做一些少量配置,例如打开关闭设备接口,配置IP、子网掩码,查看IP地址等。

ifconfig eth0 down # 关闭eth0接口
ifconfig wlan0 up # 打开wlan0接口
ifconfig eth0 192.168.1.3 netmask 255.255.255.0 # 设置IP地址和子网掩码
route add default gw 192.168.1.1 # 顺便附带设置网关命令
man ifconfig 可以看到详情。

ip命令
ip - show / manipulate routing, devices, policy routing and tunnels

ip命令用于显示/操作路由、设备、策略路由和隧道,和iw一样是较为综合而强大的命令,与之相似的原型命令可以看作是ifconfig。

[root@localhost ~]# ip --help
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
ip [ -force ] -batch filename
where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |
tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |
netns | l2tp | macsec | tcp_metrics | token }
OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
-h[uman-readable] | -iec |
-f[amily] { inet | inet6 | ipx | dnet | bridge | link } |
-4 | -6 | -I | -D | -B | -0 |
-l[oops] { maximum-addr-flush-attempts } |
-o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |
-rc[vbuf] [size] | -n[etns] name | -a[ll] }
最最常用搭配

ip link set wlan0 [up] [down]  # 设置网络接口的开启关闭

其他常用命令格式搭配可以参阅此文:https://blog.csdn.net/radkitty/article/details/3022181

iwlist命令
iwlist  - Get more detailed wireless information from a wireless interface

DXCyber409@DXCyber409:/etc/wpa_supplicant$ iwlist --help
Usage: iwlist [interface] scanning [essid NNN] [last]
[interface] frequency
[interface] channel
[interface] bitrate
[interface] rate
[interface] encryption
[interface] keys
[interface] power
[interface] txpower
[interface] retry
[interface] ap
[interface] accesspoints
[interface] peers
[interface] event
[interface] auth
[interface] wpakeys
[interface] genie
[interface] modulation

可以通过该命令来获取最后一次扫描的缓存信息:iwlist wlan0 scanning

对于USB网卡,要扫描周围的SSID广播,该命令几乎是唯一选项,因为iw支持不好。

iwconfig命令
和ifconfig是同级别的用户级管理工具,但专注于无线网络管理。

DXCyber409@DXCyber409:/etc/wpa_supplicant$ iwconfig --help
Usage: iwconfig [interface]
interface essid {NNN|any|on|off}
interface mode {managed|ad-hoc|master|...}
interface freq N.NNN[k|M|G]
interface channel N
interface bit {N[k|M|G]|auto|fixed}
interface rate {N[k|M|G]|auto|fixed}
interface enc {NNNN-NNNN|off}
interface key {NNNN-NNNN|off}
interface power {period N|timeout N|saving N|off}
interface nickname NNN
interface nwid {NN|on|off}
interface ap {N|off|auto}
interface txpower {NmW|NdBm|off|auto}
interface sens N
interface retry {limit N|lifetime N}
interface rts {N|auto|fixed|off}
interface frag {N|auto|fixed|off}
interface modulation {11g|11a|CCK|OFDMg|...}
interface commit
Check man pages for more details.
可以使用此命令来连接开放(未加密)的WI-FI网络,和查看接入点的情况。

iwconfig wlan0 essid "WIFI名称" # 连接开放的WI-FI网络
iwconfig wlan0 # 查看wlan0连接情况,如果成功连接,将在Access Point显示下一路由的MAC地址,否则表示尚未连接。
wpa_supplicant命令
wpa_supplicant  - Wi-Fi Protected Access client and IEEE 802.1X supplicant

该命令可用于WPA/WPA2-PSK/WEP加密网络的连接管理,现代WI-FI环境必备。

DXCyber409@DXCyber409:/etc/wpa_supplicant$ wpa_supplicant --help
wpa_supplicant: invalid option -- '-'
wpa_supplicant v2.6
Copyright (c) 2003-2016, Jouni Malinen j@w1.fi and contributors

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

This product includes software developed by the OpenSSL Project
for use in the OpenSSL Toolkit (http://www.openssl.org/)

usage:
wpa_supplicant [-BddhKLqqstuvW] [-P] [-g]
[-G]
-i -c [-C] [-D] [-p<driver_param>]
[-b<br_ifname>] [-e] [-f]
[-o] [-O]
[-N -i -c [-C] [-D]
[-m]
[-p<driver_param>] [-b<br_ifname>] [-I] ...]

drivers:
nl80211 = Linux nl80211/cfg80211
wext = Linux wireless extensions (generic)
wired = Wired Ethernet driver
none = no driver (RADIUS server/WPS ER)
options:
-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
-f = log output to debug file instead of stdout
-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)
-m = Configuration file for the P2P Device interface
-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)
-s = log output to syslog instead of stdout
-t = include timestamp in debug messages
-T = record to Linux tracing in addition to logging
(records all messages regardless of debug verbosity)
-u = enable DBus control interface
-v = show version
-W = wait for a control interface monitor before starting
example:
wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wpa_supplicant.conf
接下来演示如何通过wpa_supplicant连接加密的Wi-Fi网络,详细的解释以及配套的CLI交互程序使用,如有需要请详见参考文献。

博主所使用的Linux环境为Kali-Rolling,开机是会自己启动好wpa_supplicant的,因此已经提前kill掉相关进程用于演示。

首先按照手机等连接Wi-Fi的习惯,我们需要知道扫描无线网络广播并记录需要连接的SSID名称(iw命令和iwlist命令都可以完成,请查阅本文小关小节)。

根据手册我们需要一个配置文件,填入连接配置信息,sudo vim /etc/wpa_supplicant.conf

ctrl_interface=/var/run/wpa_supplicant

network={
ssid="Wifi Network 1"
psk="12345678"
}
network={
ssid="Wifi Network 2"
psk="87654321"
}
随后启动wpa_supplicant
sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf

其中守护进程启动的-B参数如果不使用,可以看到连接过程用于监控错误的发生,例如密码输入错误。

启动后通过iwconfig命令可以看到wlan0接口已经有了Access Point地址,证明已经连接。

但ifconfig wlan0没有显示IP,此时需要获取IP地址才能上网。

dhclient wlan0 之后即可打开网页测试。

补充:USB无线网卡使用wpa_supplicant时出现错误信息

DXCyber409@DXCyber409:~$ sudo wpa_supplicant -i wlan1 -c /etc/wpa_supplicant.conf
Successfully initialized wpa_supplicant
nl80211: Could not configure driver mode
nl80211: deinit ifname=wlan1 disabled_11b_rates=0
wlan1: Failed to initialize driver interface
在wpa_supplicant启动参数中补充 -D wext 参数即可。

同时在wpa_supplicant的输出中可以看到

Successfully initialized wpa_supplicant
rfkill: Cannot get wiphy information
博主的USB网卡驱动并没有使用默认的n80211驱动模式,而是换成了扩展驱动wext模式。因此并没有被归纳入系统支持设备中,这也正是iw命令不能很好的支持USB网卡的原因。

参考文献

wpa_cli命令解析:https://blog.csdn.net/jy1075518049/article/details/51015141

linux下连接无线网出现nl80211: Could not configure driver mode nl80211:

http://www.cnblogs.com/dakewei/p/7750433.html

@cisen
Copy link
Owner Author

cisen commented Nov 24, 2018

最后成功的上网办法

  1. sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
  2. 添加对应的网络
network={
	ssid="Wifi Network 1" 
	psk="12345678"
}
network={
	ssid="Wifi Network 2"
	psk="87654321"
}

编辑完:ctrl+0,enter,ctrl+x
3. 随后启动wpa_supplicant
sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
4. 查看链接情况:
ifconfig wlan0
ping baidu.com
5. 多个启动了
系统已经存在打开的多个wpa_supplicant实例,执行一下sudo killall wpa_supplicant杀死所有wpa_supplicant即可。
/etc/init.d/networking restart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant