您的位置:首页 > 编程语言 > ASP

Raspberry pi wifi热点续

2015-09-13 05:18 585 查看
上一篇介绍了用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 loiface lo inet loopback
auto eth0allow-hotplug eth0iface eth0 inet manual
auto wlan0allow-hotplug wlan0iface wlan0 inet manualwpa-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=netdevupdate_config=1
network={ ssid="SSID" psk="PASSWORD" key_mgmt=WPA-PSK}

再保存一份作为wifi AP模式的配置文件使用
pi@raspberrypi ~ $ cat /etc/network/interfaces.apauto loiface lo inet loopback
auto eth0allow-hotplug eth0iface 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 wlan0iface 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.shsudo cp /etc/network/interfaces.net /etc/network/interfacessudo /etc/init.d/networking restartecho "network mode set"

转换成wifi AP模式
pi@raspberrypi ~ $ cat ./ap.sh #!/bin/bash#ap.shsudo cp /etc/network/interfaces.ap /etc/network/interfacessudo /etc/init.d/networking restartecho "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) || trueif [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP"fisudo service hostapd startsudo cp /etc/network/interfaces.ap /etc/network/interfacessh /home/pi/nat.shexit 0

这样,raspberry pi启动的时候就是AP模式,在家里可以用脚本转换为普通wifi client模式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: