您好,登錄后才能下訂單哦!
上一篇介紹了用raspbery pi做wifi熱點(diǎn)。但是如果我把raspberry pi做成wifi熱點(diǎn)的話無法讓raspberry pi無線連接到家里的wifi連接internet了。所以為了讓raspberry pi既可以作為wifi熱點(diǎn),也可以在平時(shí)連接家里的wifi連接internet,我這里介紹一個我認(rèn)為比較好用的辦法。
思路是用shell腳本,更改一下raspberry pi的網(wǎng)絡(luò)設(shè)定,執(zhí)行腳本可以更換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
分別做成兩個腳本文件,可以執(zhí)行腳本更換模式:
轉(zhuǎn)換成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"
轉(zhuǎn)換成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之前加上一行,使之默認(rèn)啟動為AP模式,這樣在外面沒有顯示器的地方也可以通過手機(jī)或者電腦登陸raspberry pi實(shí)時(shí)更換模式了。
#!/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啟動的時(shí)候就是AP模式,在家里可以用腳本轉(zhuǎn)換為普通wifi client模式。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。