话不多说直接上代码
#!/bin/bash errcode="Device not found" func_connect_wifi(){ ?? ?echo "输入网卡设备" ?? ?read interface_dev
?? ?verifi_dev=`ifconfig|grep -ci "$interface_dev"` ?? ?if [[ $verifi_dev == 0 ]];then ?? ??? ?echo "该设备不存在" ?? ??? ?exit -1 ?? ?fi
?? ?wifi_id=`wpa_cli -i $interface_dev add_network` ?? ?echo "准备建立新连接 wifi_id = $wifi_id" ?? ?wpa_cli -i $interface_dev scan ?? ?wpa_cli -i $interface_dev scan_result ?? ?echo "当前环境WiFi信息"
?? ?echo "输入要连接的WiFi名" ?? ?read wifi_name ?? ?result=$(wpa_cli -i $interface_dev set_network $wifi_id ssid \"$wifi_name\") ?? ?echo "result = $result" ?? ?if [[ $result != "OK" ]];then ?? ??? ?echo "连接失败" ?? ??? ?wpa_cli -i wlan0 remove_network $wifi_id ?? ??? ?exit -1 ?? ?fi
?? ?echo "输入密码" ?? ?read wifi_password ?? ?result2=$(wpa_cli -i $interface_dev set_network $wifi_id psk \"$wifi_password\") ?? ?if [[ $result2 != "OK" ]];then ?? ??? ?echo "密码错误连接失败" ?? ??? ?wpa_cli -i wlan0 remove_network $wifi_id ?? ??? ?exit -1 ?? ?fi
?? ?result3=$(wpa_cli -i $interface_dev enable_network $wifi_id) ?? ?echo "result = $result" ?? ?if [[ $result3 != "OK" ]];then ?? ??? ?echo "连接失败" ?? ??? ?wpa_cli -i wlan0 remove_network $wifi_id ?? ??? ?exit -1 ?? ?fi
?? ?result4=$(wpa_cli -i $interface_dev select_network $wifi_id) ?? ?echo "result = $result4" ?? ?if [[ $result3 != "OK" ]];then ?? ??? ?echo "WiFi切换失败" ?? ??? ?exit -1 ?? ?fi ?? ?echo "WiFi已切换开始自动分配IP" ?? ?resule5=$(udhcpc -b -i $interface_dev -q) ?? ?echo "连接完成" ?? ?exit 0 } func_dele_wifi(){ ?? ?echo "输入网卡设备" ?? ?read interface_dev
?? ?verifi_dev=`ifconfig|grep -ci "$interface_dev"` ?? ?if [[ $verifi_dev == 0 ]];then ?? ??? ?echo "该设备不存在" ?? ??? ?exit -1 ?? ?fi ?? ?echo "当前WiFi列表" ?? ?wpa_cli -i $interface_dev list_network ?? ?echo "选择删除对象" ?? ?read wifi_id ?? ?wpa_cli -i $interface_dev remove_network $wifi_id } func_view_wifi(){ ?? ?echo "输入网卡设备" ?? ?read interface_dev
?? ?verifi_dev=`ifconfig|grep -ci "$interface_dev"` ?? ?if [[ $verifi_dev == 0 ]];then ?? ??? ?echo "该设备不存在" ?? ??? ?exit -1 ?? ?fi ?? ?echo "当前WiFi列表" ?? ?wpa_cli ?-i $interface_dev status } echo "连接WiFi:0 ?删除WiFi:1 查看当前WiFi状态:2" read commd case $commd in ?? ?0) ?? ?func_connect_wifi ;; ?? ? ?? ?1) ?? ?func_dele_wifi ;; ?? ? ?? ?2) ?? ?func_view_wifi ;;
esac ?
本文只提供一种方法当然还有其他写法,仅供参考根据环境不同选择食用
|