用shell指令碼實現linux系統上wifi模式(STA和soft AP)的轉換

來源:互聯網
上載者:User

標籤:des   style   blog   http   使用   os   

轉載請註明出處:http://blog.csdn.net/hellomxj1/

功能:在linux系統上實現wifi STA與AP功能的轉換

實現成果:1、添加wifi密碼賬戶add_wifi_account;

          2、wifi兩種模式啟動的指令碼wifi_start;

             3、協助資訊README_WIFI_START;


具體實現過程如下:

添加wifi密碼賬戶add_wifi_account

  1 #!/bin/sh                                                                                                                                  2   3 echo "Add Wifi Account ..."  4   5 if [ ! -e /usr/firmware/wifi/wpa_supplicant.conf]  6 then  7     echo "There is not wpa_supplicant.conf!!!"  8     exit  9 fi 10     echo network={ >>/etc/wifi/wpa_supplicant.conf 11     echo ssid=\"$1\" >>/etc/wifi/wpa_supplicant.conf 12     echo psk=\"$2\" >>/etc/wifi/wpa_supplicant.conf 13     echo key_mgmt=$3 >>/etc/wifi/wpa_supplicant.conf 14     echo } >>/etc/wifi/wpa_supplicant.conf 15  16  17     echo "ssid=\"$1\"" 18     echo "psk=\"$2\"" 19     echo "key_mgmt=$3" 20  21  22 echo "Finshed!"~                    


該部分主要是實現將賬戶和密碼添加到wpa的設定檔,以便於使用sta模式啟動能自動連接網路,若果該設定檔中有多個賬戶資訊,將會從依次串連,如果其中一個成功,將不會往下執行。


wifi兩種模式啟動的指令碼wifi_start

1 #!/bin/sh  2   3 echo "Start wifi ..."  4   5 if [ ! -e /var/run/wpa_supplicant]  6 then  7 mkdir -p /var/run/wpa_supplicant  8 fi  9  10 busybox ifconfig wlan0 up 11  12 ps -fe|grep wpa_supplicant |grep -v grep 13  14 if [ $? -ne 0 ] 15 then 16 wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wifi/wpa_supplicant.conf& 17 fi 18  19 echo "######$1" 20  21 if [ "$1" = "ap" ] 22 then 23  24     echo "Start wifi AP..." 25  26     if [ ! $# == 2 ] 27     then 28         echo "Please input : start-wifi ap 192.168.1.xx(1-19)" 29         exit 30     fi 31  32     if [ ! -e /var/lib/misc ] 33     then 34     mkdir -p /var/lib/misc 35     fi 36  37     if [ ! -e /var/lib/misc/udhcpd.leases ] 38     then 39     touch /var/lib/misc/udhcpd.leases 40     fi                                                                                                                                    41  42     ifconfig wlan0 down 43  44     result=`cat /sys/module/bcmdhd/parameters/firmware_path` 45  46     if [ "$result" != "/usr/firmware/wifi/fw_43341_apsta.bin" ] 47     then 48     echo "/usr/firmware/wifi/fw_43341_apsta.bin">/sys/module/bcmdhd/parameters/firmware_path 49     fi 50  51     ifconfig wlan0 $2 up                                                                                                                  52  53     echo "Start hostapd ..." 54  55     ps -fe|grep hostapd |grep -v grep 56  57     if [ $? -eq 0 ] 58     then 59         ps -ef | grep hostapd | grep -v grep | awk '{print $1}' | sed -e "s/^/kill -9 /g" | sh - 60     fi 61  62     hostapd /etc/wifi/hostapd.conf& 63  64     echo "Start udhcpd ..." 65  66     ps -fe|grep udhcpd |grep -v grep 67  68     if [ $? -eq 0 ] 69     then 70         ps -ef | grep udhcpd | grep -v grep | awk '{print $1}' | sed -e "s/^/kill -9 /g" | sh - 71     fi 72  73     udhcpd -fS /etc/udhcpd.conf& 74  75     echo "Wifi AP finshed!" 76  77 elif [ "$1" = "sta" ] 78 then 79     ifconfig wlan0 down 80  81     result=`cat /sys/module/bcmdhd/parameters/firmware_path` 82  83     if [ "$result" != "/usr/firmware/wifi/fw_43341.bin" ] 84     then 85     echo "/usr/firmware/wifi/fw_43341.bin">/sys/module/bcmdhd/parameters/firmware_path 86     fi 87  88     ifconfig wlan0 up 89  90     udhcpc -i wlan0 91  92 else 93  94     echo "Error!! Please input again!" 95  96 fi

 

這裡主要分為兩個模組,一個是sta模式的啟動直接輸入./wifi_start sta;即可啟動,另一個為soft AP模式直接輸入:./wifi_start ap 192.168.2.10;即可進入soft AP模式,考慮到兩種模式的不斷轉換的問題,我在執行的前期會對進程進行檢查,將以前開啟的進程進行關閉,一邊本次開啟能夠正常運行;


協助資訊README_WIFI_START

  1 <1>Introduction                                                                                                                            2   3     This document describes how to start wifi,and introduces the function and use methods of add-wifi-account and wifi-start;And add-        wifi-account and wifi-start in the "/etc/init.d" directory.  4   5 <2>How to operate  6   7 First,Add the available WiFi account  8   9     #/etc/init.d/add-wifi-account "JZ_Guest" "#wwwingenic*" "WPA-PSK" 10  11     Notes: "JZ_Guest" is the ssid of the network that you want to connect; 12            "#wwwingenic* is the password of the network; 13            " WPA-PSK"  is the encryption method of the network; 14  15 Second,Start wifi 16  17     /*Start wifi with STA model*/ 18  19     #/etc/init.d/wifi-start sta 20  21     /*Start wifi with AP model*/ 22  23     #/etc/init.d/wifi-start ap 192.168.1.10 24  25     Notes: 192.168.1.10 is ip of newton,you can use ip address from 192.168.1.1 <----> 192.168.1.19

這裡主要是我的指令碼的使用說明,方便使用;



時間比較緊,功能的實現可能會有問題,希望能給大家多多給出建議。


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.