安裝配置
最近用到產品無線功能模塊自動化無線連接測試,這次做一個環境部署配置總結,在centos7.7系統內核版本3.10,使用的是RTL8812AU無線芯片網卡,Realtek官方有提供Linux驅動沒法編譯,國外大佬做了一個可以用於3.1內核的改版驅動,需網上下載:rtl8812au.zip驅動包。
使用make、make install命令編譯安裝,安裝完后最好reboot一下,如果在編譯過程中出現問題,一般來說都是依賴沒裝好,嘗試用以下命令解決:
yum update (更新yum軟件倉庫)
yum install gcc (安裝gcc)
安裝完后,查看內核是否有加載的8812au模塊:
[root@Server ~]# lsmod | grep 8812
8812au 1011830 0
不支持iw命令,例如輸入iw dev會顯示“nl80211 not found.”,並不影響使用。
使用ip link命令查看網卡設備名,可見下圖ens35u1就是無線網卡設備名。
啟動無線網卡:ifconfig ens35u1 up
設置WIFI熱點:
首先如果還沒安裝wpa_supplicant的話,請先運行yum install wpa_supplicant
然后修改配置文件:
# vim /etc//wpa_supplicant/wpa_supplicant.conf
ctrl_interface=/var/run/wpa_supplicant/
update_config=1
device_name=RTL8192CU
manufacturer=Realtek
model_name=RTW_STA
model_number=WLAN_CU
serial_number=12345
device_type=1-0050F204-1
os_version=01020300
config_methods=virtual_display virtual_push_button keypad
以上內容就是添加到配置文件里。
服務端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接受多個命令行參數,特別是:
-B:在后台執行
-c:文件名 -路徑配置文件
-i:接口 - 監聽的接口
-D:指定驅動
關於支持的驅動程序的列表,請參考wpa_supplicant -h的輸出結果。
- nl80211 是當前的標准,但不是所有的無線芯片的模塊支持。
- wext 目前已過時,但仍得到廣泛支持。
使用 -D 開關來指定驅動:
# wpa_supplicant -B -i interface -c configuration_file -D driver
啟動后台:
wpa_supplicant -D wext -i ens35u1 -c /etc/wpa_supplicant/wpa_supplicant.conf -B
開機啟動:
/usr/lib/systemd/system/wpa_supplicant.service
/usr/sbin/wpa_supplicant -u -f /var/log/wpa_supplicant.log -D wext -i ens35u1 -c /etc/wpa_supplicant/wpa_supplicant.conf
systemctl enable wpa_supplicant
wpa_cli交互模式的命令如下表:
Full command |
Short command |
Description |
status |
stat |
displays the current connection status |
disconnect |
disc |
prevents wpa_supplicant from connecting to any access point |
quit |
q |
exits wpa_cli |
terminate |
term |
kills wpa_supplicant |
reconfigure |
recon |
reloads wpa_supplicant with the configuration file supplied (-c parameter) |
scan |
scan |
scans for available access points (only scans it, doesn't display anything) |
scan_result |
scan_r |
displays the results of the last scan |
list_networks |
list_n |
displays a list of configured networks and their status (active or not, enabled or disabled) |
select_network |
select_n |
select a network among those defined to initiate a connection (ie select_network 0) |
enable_network |
enable_n |
makes a configured network available for selection (ie enable_network 0) |
disable_network |
disable_n |
makes a configured network unavailable for selection (ie disable_network 0) |
remove_network |
remove_n |
removes a network and its configuration from the list (ie remove_network 0) |
add_network |
add_n |
adds a new network to the list. Its id will be created automatically |
set_network |
set_n |
shows a very short list of available options to configure a network when supplied with no parameters. See next section for a list of extremely useful parameters to be used with set_network and get_network. |
get_network |
get_n |
displays the required parameter for the specified network. See next section for a list of parameters |
save_config |
save_c |
saves the configuration |
設置網絡的基本格式:set_network <network id> <key> <parameter> [<parameter>]
顯示網絡信息的基本格式:get_network <network id> <key>
相應的參數如下表:
Key |
Description |
Parameters |
ssid |
Access point name |
string |
id_str |
String identifying the network |
string |
priority |
Connection priority over other APs |
number (0 being the default low priority) |
bssid |
Mac address of the access point |
mac address |
scan_ssid |
Enable/disbale ssid scan |
0, 1, 2 |
key_mgmt |
Type of key management |
WPA-PSK, WPA_EAP, None |
pairwise |
Pairwise ciphers for WPA |
CCMP, TKIP |
group=TKIP |
Group ciphers for WPA |
CCMP, TKIP, WEP104, WEP40 |
psk |
Pre-Shared Key (clear or encrypted) |
string |
wep_key0 |
WEP key (up to 4: wep_key[0123]) |
string |
eap |
Extensible Authentication Protocol |
MD5, MSCHAPV2, OTP, GTC, TLS, PEAP, TTLS |
identity |
EAP identity string |
string |
password |
EAP password |
string |
ca_cert |
Pathname to CA certificate file |
/full/path/to/certificate |
client_cert |
Pathname to client certificate |
/full/path/to/certificate (PEM/DER) |
private_key |
Pathname to a client private key file |
/full/path/to/private_key (PEM/DER/PFX) |
常見例子:
1、連接無加密的AP
>add_network (It will display a network id for you, assume it returns 0)
>set_network 0 ssid ‘"test"’
>set_network 0 key_mgmt NONE
>enable_network 0
>quit
2、連接WEP加密AP
>add_network (assume return 1)
>set_network 1 ssid ‘"test"’
>set_network 1 key_mgmt NONE
>set_network 1 wep_key0 "'your ap password"'
>enable_network 1
3、連接WPA-PSK/WPA2-PSK加密的AP
>add_network (assume return 2)
>set_network 2 ssid ‘"test"’
>set_network 2 psk '"your pre-shared key"'
>enable_network 2
到此,wifi模塊就能連接上AP了。
3、以上是通過命令行工具wpa_cli來實現wifi網絡的連接。當然,也可以通過wpa_supplicant的配置文件來實現連接。
再回顧下運行wpa_supplicant時執行的命令:
/system/bin/wpa_supplicant -d -Dwext -iens35u1 -c/etc/wpa_supplicant/wpa_supplicant.conf
我們在執行時加上了-c/etc/wpa_supplicant/wpa_supplicant.conf,可以將我們要連接的AP的設置以一定的格式寫入wpa_supplicant.conf配置文件中即可。
例如:
ctrl_interface=/var/run/wpa_supplicant/
network={
ssid="my access point"
proto=WPA
key_mgmt=WPA-PSK
psk='"you pass words"'
}
配置hostap軟路由
- 官網下個特別版Hostap,這個程序可以在官方驅動包里找到,位置是:\Linux\wpa_supplicant_hostapd\wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz
- 找到后把它復制到當前目錄,如果之前安裝過hostapd,可以使用yum remove hostapd命令,把原版的先卸載掉。
解壓:
# tar -xzvf wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz
# cd wpa_supplicant_hostapd-0.8_rtw_r7475.20130812/hostapd
# make & make install
修改配置文件:
# vim rtl_hostapd.conf
interface=ens35u1
ctrl_interface=/var/run/hostapd
ssid=CentOS_Test
channel=6
wpa=2
wpa_passphrase=12345678
#bridge=br0
eap_server=1
wps_state=2
uuid=12345678-abc5-def0-1234-56789abcdef0
device_name=RTL8192CU
manufacturer=Realtek
model_name=RTW_SOFTAP
model_number=WLAN_CU
serial_number=12345
device_type=6-0050F204-1
os_version=01020300
config_methods=label display push_button keypad
driver=rtl871xdrv
beacon_int=100
hw_mode=g
ieee80211n=1
wme_enabled=1
ht_capab=[SHORT-GI-20][SHORT-GI-40][HT40+]
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
max_num_sta=8
wpa_group_rekey=86400
上面這個配置文件中,紅色部分是需要修改的:
1. interface=無線網卡設備名
2. ssid=自定義熱點名
3. wpa_passphrase=密碼
4. bridge=網橋名 (如以網橋方式連接的話,要填上網橋名,否則必須注釋掉)
5. uuid=通用識別碼
詳細版本在官方驅動程序包的如下位置:
配置文件:\Linux\wpa_supplicant_hostapd\rtl_hostapd_2G.conf
PDF說明:\Linux\document\Quick_Start_Guide_for_SoftAP.pdf
# chmod +x rtl_hostapd.conf
# ./hostapd rtl_hostapd.conf -B (以后台方式啟動hostap)
最后,用手機掃描WIFI,會發現有一個名為CentOS_Test的熱點,恭喜成功!
綜合實踐
- WiFi開啟
先配置好無線路由器SSID、加密方式等。
# ifconfig ens35u1 up ( 啟動無線網卡)
# wpa_supplicant -D wext -i ens35u1 -c /etc/wpa_supplicant/wpa_supplicant.conf -B (啟動服務端)
根據配置文件/etc/wpa_supplicant.conf來初始化ens35u1接口
如果wpa_supplicant.conf文件為空,則不會自動連接,需要連接一次網絡才行
- 掃描熱點
命令如下:
wpa_cli -i ens35u1 scan //掃描附近熱點
wpa_cli -i ens35u1 scan_result //打印熱點信息
- 連接熱點
# wpa_cli -i ens35u1 add_network // 添加一個網絡連接,並返回網絡ID號,如果wpa_supplicant.conf文件為空,則返回0,表示第一個熱點
作用:用來區分用戶連接過的每個熱點
3.1以連接yj-hello_5G為例
wpa_cli -i ens35u1 set_network 1 ssid '"yj-hello_5G"' //設置ID為0的熱點SSID
wpa_cli -i ens35u1 set_network 1 psk '"yj-hello_5G"' //設置ID為0的熱點的密碼
wpa_cli -i ens35u1 key_mgmt WPA-PSK
PS:如果是沒有加密的公共網絡,則為NONE:
wpa_cli -i ens35u1 set_network 0 key_mgmt NONE
繼續輸入:
wpa_cli -i ens35u1 set_network 0 priority 2 //設置當前網絡優先級,默認為2
wpa_cli -i ens35u1 set_network 0 scan_ssid 1 //設置ssid默認為1即可
wpa_cli -i ens35u1 enable_network 1 //啟動連接ID為0的網絡
wpa_cli -i ens35u1 select_network 1 //選擇網絡0(這樣做的好處在於,可以取消先前的其它網絡連接)
udhcpc -b -i ens35u1 -q 或 dhclient ens35u1 或 配置靜態IP (ifconfig ens35u1 192.168.0.19 netmask 255.255.255.0)
ping路由器網關:
- 保存連接配置
wpa_cli -i ens35u1 save_config //保存當前連接的網絡配置到wpa_supplicant.conf文件中,使得下次自動連接
保存后的wpa_supplicant.conf文件如下所示:
查看無線網卡當前連接狀態:
# wpa_cli -i ens35u1 status
查看當前連接的是哪個網絡:
# wpa_cli -i wlan0 list_network
打印如下,可以看到當前連接的ID號為1:
查看當前速率:
[root@Server ~]# iwlist ens35u1 rate
ens35u1 4 available bit-rates :
1 Mb/s
2 Mb/s
5.5 Mb/s
11 Mb/s
Current Bit Rate:867 Mb/s
查看當前信道:
[root@Server ~]# iwlist ens35u1 channe | grep Current
Current Frequency:5.745 GHz (Channel 149)
查看更詳細信息:
以上的信息數據,自動化測試,用例會到獲取速率值或信道值或MAC地址,需要腳本處理字符數據截取出來值。
- 斷開熱點
wpa_cli -i wlan0 disable_network 0 //與id0的網絡進行斷開
wpa_cli -i wlan0 remove_network 0 //將id0的網絡移除掉,必須先斷開才行
wpa_cli -i wlan0 save_config //並更新wpa_supplicant.conf文件
配置后的wpa_supplicant.conf文件如下所示:
ctrl_interface=/var/run/wpa_supplicant/
update_config=1
device_name=RTL8192CU
manufacturer=Realtek
model_name=RTW_STA
model_number=WLAN_CU
serial_number=12345
device_type=1-0050F204-1
os_version=01020300
config_methods=virtual_display virtual_push_button keypad
移除后,只缺少了當前SSID熱點數據,以上內容還是默認會保留的。
- 關閉WIFI
# killall udhcpc (如果ens35u1接口配置靜態IP,此命令不使用)
# killall wpa_supplicant
# ifconfig ens35u1 down
備注:掃描過程中偶爾會出現如下信息:
Successfully initialized wpa_supplicant
rfkill: Cannot get wiphy information
ioctl[SIOCSIWAP]: Operation not permitted
似乎有一點報錯,但實際上對使用並沒什么影響。
還可以tail -f /var/log/wap_supplicant.log動態監控查看無線掃描、連接過程打印出來的日志數據。