Raspberry pi wifi熱點續

上一篇介紹了用raspbery pi做wifi熱點。但是如果我把raspberry pi做成wifi熱點的話無法讓raspberry pi無線連接到家裏的wifi連接internet了。所以爲了讓raspberry pi既可以作爲wifi熱點,也可以在平時連接家裏的wifi連接internet,我這裏介紹一個我認爲比較好用的辦法。


思路是用shell腳本,更改一下raspberry pi的網絡設定,執行腳本可以更換wifi AP模式和wifi client模式。


首先需要配置/etc/networks/interface文件,

第一個是爲普通wifi client模式使用的。

pi@raspberrypi ~ $ cat /etc/network/interfaces.net 

auto lo

iface lo inet loopback


auto eth0

allow-hotplug eth0

iface eth0 inet manual


auto wlan0

allow-hotplug wlan0

iface wlan0 inet manual

wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf


pi@raspberrypi ~ $ sudo cat /etc/wpa_supplicant/wpa_supplicant.conf 

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

update_config=1


network={

ssid="SSID"

psk="PASSWORD"

key_mgmt=WPA-PSK

}


再保存一份作爲wifi AP模式的配置文件使用

pi@raspberrypi ~ $ cat /etc/network/interfaces.ap

auto lo

iface lo inet loopback


auto eth0

allow-hotplug eth0

iface eth0 inet manual


#auto wlan0

#allow-hotplug wlan0

#iface wlan0 inet manual

#wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf


#auto wlan1

#allow-hotplug wlan1

#iface wlan1 inet manual

#wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf


allow-hotplug wlan0

iface wlan0 inet static

    address 192.168.20.1

    netmask 255.255.255.0



#up iptables-restore < /etc/network/iptables

分別做成兩個腳本文件,可以執行腳本更換模式:

轉換成wifi client模式

pi@raspberrypi ~ $ cat ./net.sh 

#!/bin/sh

#net.sh

sudo cp /etc/network/interfaces.net /etc/network/interfaces

sudo /etc/init.d/networking restart

echo "network mode set"


轉換成wifi AP模式

pi@raspberrypi ~ $ cat ./ap.sh 

#!/bin/bash

#ap.sh

sudo cp /etc/network/interfaces.ap /etc/network/interfaces

sudo /etc/init.d/networking restart

echo "set to ap mode"

在/etc/rc.local文件最後一行exit 0之前加上一行,使之默認啓動爲AP模式,這樣在外面沒有顯示器的地方也可以通過手機或者電腦登陸raspberry pi實時更換模式了。


#!/bin/sh -e

#

# rc.local

#

# This script is executed at the end of each multiuser runlevel.

# Make sure that the script will "exit 0" on success or any other

# value on error.

#

# In order to enable or disable this script just change the execution

# bits.

#

# By default this script does nothing.


# Print the IP address

_IP=$(hostname -I) || true

if [ "$_IP" ]; then

  printf "My IP address is %s\n" "$_IP"

fi

sudo service hostapd start

sudo cp /etc/network/interfaces.ap /etc/network/interfaces

sh /home/pi/nat.sh

exit 0



這樣,raspberry pi啓動的時候就是AP模式,在家裏可以用腳本轉換爲普通wifi client模式。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章