温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Raspberry pi wifi热点续

发布时间:2020-07-16 19:20:17 来源:网络 阅读:1113 作者:fanglinxun 栏目:开发技术

上一篇介绍了用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模式。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI