linux下应用wifi,AP侧运行程序hostapd,客户端运行wpa_supplicant。官网:http://w1.fi/。
不是所有的无线网卡都支持所有模式,可以查阅Wireless adapters/Chipset table(https://deviwiki.com/wiki/Wireless_adapters/Chipset_table了解芯片是否支持相应模式。
1. wpa_supplicant
wpa_supplicant本是开源项目源码,被谷歌修改后加入Android移动平台,它主要是用来支持WEP,WPA/WPA2和WAPI无线协议和加密认证的,而实际上的工作内容是通过socket(不管是wpa_supplicant与上层还是wpa_supplicant与驱动都采用socket通讯)与驱动交互上报数据给用户,而用户可以通过socket发送命令给wpa_supplicant调动驱动来对WiFi芯片操作。 简单的说,wpa_supplicant就是WiFi驱动和用户的中转站外加对协议和加密认证的支持。
目前可以使用wireless-tools 或wpa_supplicant工具来配置无线网络。请记住重要的一点是,对无线网络的配置是全局性的,而非针对具体的接口。
wpa_supplicant是一个较好的选择,但缺点是它不支持所有的驱动。请浏览wpa_supplicant网站获得它所支持的驱动列表。另外,wpa_supplicant目前只能连接到那些你已经配置好ESSID的无线网络。wireless-tools支持几乎所有的无线网卡和驱动,但它不能连接到那些只支持WPA的AP。
经过编译后的wpa_supplicant源程序可以看到两个主要的可执行工具:wpa_supplicant和wpa_cli。wpa_supplicant是核心程序,它和wpa_cli的关系就是服务和客户端的关系:后台运行wpa_supplicant,使用wpa_cli来搜索、设置、和连接网络。
wpa_supplicant [-BddfhKLqqtuvW] [-P<pid file>] [-g<globalctrl>] \
[-G<group>] \
-i<ifname> -c<config file> [-C<ctrl>] [-D<driver>][-p<driver_param>] \
[-b<br_ifname> [-MN -i<ifname> -c<conf>[-C<ctrl>] [-D<driver>] \
[-p<driver_param>] [-b<br_ifname>] [-m<P2P Device configfile>] ...
选项
-b= 可选的桥接接口名称
-B= 后台运行
-c= 配置文件路径
-C= ctrl_interface 参数(仅在-c不使用的时候使用)
-i= 接口名称
-d= 增加调试信息详细程度(-dd显示更多)
-D= 驱动名称
-f= 将日志输出到默认日志位置(通常为/ tmp)
-g= 全局ctrl_interface
-G= 全局 ctrl_interface group
-K= 包括密钥信息在调试中输出
-t= 调试信息添加时间戳
-h= 显示帮助文档
-L= 显示许可证
-p= 驱动程序参数
-P= PID 文件
-q= 减少调试信息详细程度(-qq更少)
-u= 驱动 DBus control interface
-v= 显示版本
-W= 在启动之前等待control interface monitor
-M= 开始描述匹配接口
-N= 开始描述新接口
-m= P2P Device的配置文件
驱动
nl80211 = Linux nl80211/cfg80211 wext = Linux wireless extensions (generic) wired = wpa_supplicant wired Ethernet driver roboswitch = wpa_supplicant Broadcom switch driver bsd= BSD 802.11 support (Atheros, etc.) ndis = Windows NDIS driver
wpa_cli
wpa_cli是一个基于文本的前端程序,用于与wpa_supplicant 进行交互。它用于查询当前状态,更改配置,触发事件并请求交互式用户输入。
wpa_cli支持两种模式:交互式和命令行。
wpa_cli命令:
status = 获取当前WPA/EAPOL/EAP 状态
mib= 获取MIB变量
help = 显示帮助文档
interface [ifname] = 连接接口
level <debug level> = 改变debug等级
license = 显示wpa_cli证书
logoff = IEEE 802.1X EAPOL state machine logoff
logon = IEEE 802.1X EAPOL state machine logon
set= 设置变量
pmksa = 显示PMKSA缓存
reassociate= 强制重新连接
reconfigure =强制wpa_supplicant重新读取配置文件
preauthenticate <BSSID> = 强制预先认证
identity <network id> <identity> = 为SSID配置身份
password <network id> <password> =为SSID配置密码
pin<network id> <pin> = 为SSID配置引脚
otp<network id> <password> = 为SSID配置临时密钥
passphrase <network id> <passphrase> =为SSID配置私钥
bssid <network id> <BSSID> = 为SSID设置首选BSSID
list_networks = 列举已经配置的网络
select_network <network id> = 选择网络(关闭其他)
enable_network <network id> = 启用网络
disable_network <network id> = 禁用网络
add_network = 添加网络
remove_network <network id> = 删除网络
set_network <network id> <variable> <value> =设置网络变量
get_network<network id> <variable> = 获取网络变量
save_config = 保存当前配置
disconnect = 断开连接,等待重新连接命令
scan = 扫描
scan_results = 扫描结果
get_capability <eap/pairwise/group/key_mgmt/proto/auth_alg> = 获取能力
terminate = 终止wpa_supplicant
quit = 退出wpa_cli
wpa_cli [-p<path to ctrl sockets>][-i<ifname>] [-hvB] [-a<action file>] \ [-P<pid file>] [-g<global ctrl>] [command..] -h= 显示使用文档 -v=显示版本信息 -a= 以守护进程方式运行 -B= 后台运行 默认套接字路径: /var/run/wpa_supplicant 默认接口:在套接字路径中找到的第一个接口
wpa_passphrase
使用wpa_passphrase快速连接到其SSID已知的网络,wpa_passphrase是一种命令行工具,可生成wpa_supplicant所需的最小配置。
$ wpa_passphrase MYSSID 密码
2. 模式切换
参考明远智睿MY-IMX6-EK200:MYZRIMX6EK200RTL8188EUSWIFIAP
参考正点原子:
#!/bin/sh #正点原子@ALIENTEK #USB WIFI脚本 #功能:脚本支持station模式、softap模式、bridge模式相互切换(若相互切换不成功,请重启板子) #使用方法说明 usage_and_exit() { echo "" echo "*****************************************************************************************" echo "*usage: ${0##*/} [-m mode] [-i ssid] [-p psk] [-d device] [-e ethernet] [-h]*" echo "* *" echo "*station mode eg:${0##*/} -m station -i ssid -p psk -d wlan0 *" echo "* *" echo "*softap mode eg: ${0##*/} -m softap -d wlan0 *" echo "* *" echo "*bridge mode eg:${0##*/} -m bridge -d wlan0 -e eth0 *" echo "*eg: ${0##*/} -m mode mode=[ station | softap | bridge ] *" echo "*****************************************************************************************" echo "" return } #使用方法举例: #source ./alientek_usb_wifi_setup.sh -m bridge -d wlan0 -e eth0 #source ./alientek_usb_wifi_setup.sh -m station -i ALIENTEK -p 15902020353 -d wlan0 #source ./alientek_usb_wifi_setup.sh -m softap -d wlan0 #版本信息 version() { echo "version 1.0" echo "作者:DZM@ALIENTEK" } #获取参数 while [ $# -gt 0 ]; do case $1 in --help | -h) usage_and_exit $0;break;; -m) shift; mode=$1; shift; ;; -i) shift; ssid=$1; shift; ;; -p) shift; psk=$1; shift; ;; -d) shift; device=$1; shift; ;; -e) shift; ethernet=$1; shift; ;; --version) version $0; break;; *) usage_and_exit; break; ;; esac done #判断参数 if [ -n "$mode" ]; then echo "您的WIFI配置信息是:" echo "mode : $mode" fi if [ -n "$ssid" ]; then echo "ssid : $ssid" echo "psk : $psk" fi if [ -n "$device" ]; then echo "device: $device" fi if [ -n "$ethernet" ]; then echo "ethernet : $ethernet" fi #kill掉相关进程 processkill() { a=$(ps -aux |grep -E "hostapd" | grep -v grep | awk '{print $2}') if [ -n "$a" ];then kill -9 $a fi a=$(ps -aux |grep -E "udhcpd" | grep -v grep | awk '{print $2}') if [ -n "$a" ];then kill -9 $a fi a=$(ps -aux |grep -E "wpa_supplicant" | grep -v grep | awk '{print $2}') if [ -n "$a" ];then kill -9 $a fi a=$(ps -aux |grep -E "udhcpc" | grep -v grep | awk '{print $2}') if [ -n "$a" ];then kill -9 $a fi } #创建配置wifi的信息 touch /$PWD/wifi.conf wifi_conf="/$PWD/wifi.conf" #bridge Mode(桥接模式) if [ "$mode" == "bridge" ]; then ifconfig $device down ifconfig $device up ifconfig $ethernet down ifconfig $ethernet up sleep 2 if [ "$WIFI_MODE" == "station" ]; then wpa_supplicant -B -D wext -i $device -c /etc/wpa_supplicant.conf fi processkill sleep 1 sync echo -ne "interface=wlan0\nssid=alientek_bridge\ndriver=rtl871xdrv\nchannel=6\nhw_mode=g\nignore_broadcast_ssid=0\n auth_algs=1\nwpa=3\nwpa_passphrase=12345678\nwpa_key_mgmt=WPA-PSK\nwpa_pairwise=TKIP\nrsn_pairwise=CCMP\nbridge=br0" > $wifi_conf rm -rf /var/lib/misc/* touch /var/lib/misc/udhcpd.leases udhcpd -fS /etc/udhcpd.conf & ifconfig $device 0.0.0.0 brctl addbr br0 ifconfig $ethernet 0.0.0.0 brctl addif br0 $ethernet brctl addif br0 $device ifconfig br0 192.168.1.39 netmask 255.255.255.0 hostapd $wifi_conf -B export WIFI_MODE=bridge fi #softap Mode(热点模式) if [ "$mode" == "softap" ]; then processkill sleep 1 sync echo -ne "interface=wlan0\nssid=alientek_softap\ndriver=rtl871xdrv\nchannel=6\nhw_mode=g\nignore_broadcast_ssid=0\n auth_algs=1\nwpa=3\nwpa_passphrase=12345678\nwpa_key_mgmt=WPA-PSK\nwpa_pairwise=TKIP\nrsn_pairwise=CCMP" > $wifi_conf a=$(ifconfig |grep -E "br0" | grep -v grep | awk '{print $0}') if [ -n "$a" ];then brctl delif br0 $device brctl delif br0 $device ifconfig br0 down fi rm -rf /var/lib/misc/* touch /var/lib/misc/udhcpd.leases ifconfig $device up sleep 2 ifconfig $device 192.168.1.38 netmask 255.255.255.0 udhcpd -fS /etc/udhcpd.conf & hostapd $wifi_conf -B export WIFI_MODE=softap fi #station Mode(上网模式) if [ "$mode" == "station" ]; then processkill sleep 1 sync a=$(ifconfig |grep -E "br0" | grep -v grep | awk '{print $0}') if [ -n "$a" ];then brctl delif br0 $device brctl delif br0 $device ifconfig br0 down fi echo -ne "ctrl_interface=/var/run/wpa_supplicant\n update_config=1\nnetwork={\nssid=\"$ssid\"\npsk=\"$psk\"\n}\n" > $wifi_conf rm -rf /var/lib/misc/* ifconfig eth0 down ifconfig eth1 down ifconfig waln0 down ifconfig wlan0 up sleep 2 wpa_supplicant -B -D wext -i $device -c $wifi_conf udhcpc -R -b -i wlan0 route add default gw 192.168.1.1 export WIFI_MODE=station fi #删除wifi.conf rm -rf /$PWD/wifi.conf case $mode in softap|station|bridge)echo "WIFI设置$mode模式完成!" ;; esac #卸载环境变量 unset device unset wifi_conf unset a unset ethernet unset mode unset ssid unset psk sync
# Sample udhcpd configuration file (/etc/udhcpd.conf) # The start and end of the IP lease block start 192.168.111.20 #default: 192.168.0.20 end 192.168.111.254 #default: 192.168.0.254 # The interface that udhcpd will use interface wlp4s0 #default: eth0 # The maximim number of leases (includes addressesd reserved # by OFFER's, DECLINE's, and ARP conficts #max_leases 254 #default: 254 # If remaining is true (default), udhcpd will store the time # remaining for each lease in the udhcpd leases file. This is # for embedded systems that cannot keep time between reboots. # If you set remaining to no, the absolute time that the lease # expires at will be stored in the dhcpd.leases file. #remaining yes #default: yes # The time period at which udhcpd will write out a dhcpd.leases # file. If this is 0, udhcpd will never automatically write a # lease file. (specified in seconds) #auto_time 7200 #default: 7200 (2 hours) # The amount of time that an IP will be reserved (leased) for if a # DHCP decline message is received (seconds). #decline_time 3600 #default: 3600 (1 hour) # The amount of time that an IP will be reserved (leased) for if an # ARP conflct occurs. (seconds #conflict_time 3600 #default: 3600 (1 hour) # How long an offered address is reserved (leased) in seconds #offer_time 60 #default: 60 (1 minute) # If a lease to be given is below this value, the full lease time is # instead used (seconds). #min_lease 60 #defult: 60 # The location of the leases file #lease_file /var/lib/misc/udhcpd.leases #defualt: /var/lib/misc/udhcpd.leases # The location of the pid file #pidfile /var/run/udhcpd.pid #default: /var/run/udhcpd.pid # Everytime udhcpd writes a leases file, the below script will be called. # Useful for writing the lease file to flash every few hours. #notify_file #default: (no script) #notify_file dumpleases # <--- useful for debugging # The following are bootp specific options, setable by udhcpd. #siaddr 192.168.0.22 #default: 0.0.0.0 #sname zorak #default: (none) #boot_file /var/nfs_root #default: (none) # The remainer of options are DHCP options and can be specifed with the # keyword 'opt' or 'option'. If an option can take multiple items, such # as the dns option, they can be listed on the same line, or multiple # lines. The only option with a default is 'lease'. #Examles opt dns 114.114.114.114 8.8.8.8 option subnet 255.255.255.0 opt router 192.168.111.11 opt wins 192.168.111.10 option dns 129.219.13.81 # appened to above DNS servers for a total of 3 option domain local option lease 864000 # 10 days of seconds # Currently supported options, for more info, see options.c #opt subnet #opt timezone #opt router #opt timesrv #opt namesrv #opt dns #opt logsrv #opt cookiesrv #opt lprsrv #opt bootsize #opt domain #opt swapsrv #opt rootpath #opt ipttl #opt mtu #opt broadcast #opt wins #opt lease #opt ntpsrv #opt tftp #opt bootfile #opt wpad # Static leases map #static_lease 00:60:08:11:CE:4E 192.168.0.54 #static_lease 00:60:08:11:CE:3E 192.168.0.44