树莓派开机自动发射热点

由于我的是树莓派3, 自带wifi网卡,然后又加了一个usb网卡

现在分配的是, 如果检测到usb无线网卡插入,那么就使用usb无线网卡发射热点

如果eth0或者wlan0任何一个有网的情况下, 做路由转发,使热点能连上网

安装依赖

sudo apt install hostapd dnsmasq

配置hostapd

#/etc/hostapd/hostapd.conf

# Basic configuration    
interface=wlan1
ssid=TestWifiAP
channel=1
#bridge=br0

# WPA and WPA2 configuratio
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=3
wpa_passphrase=12345678
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

# Hardware configuration
driver=nl80211
#driver=rtl871xdrv
#ieee80211n=1
hw_mode=g
#device_name=RTL8192CU
#manufacturer=Realtek

配置dnsmasq

# /etc/dnsmasq.conf

#interface=wlan1
bind-interfaces
server=114.114.114.114
server=8.8.8.8
domain-needed
bogus-priv
dhcp-range=10.10.10.10,10.10.10.100,12h

配置interfaces

# /etc/network/interfaces

auto lo
iface lo inet loopback

auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

如果网卡分配有问题,请配置70-persistent-net.rules文件, mac地址记得改掉

# /etc/udev/rules.d/70-persistent-net.rules

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="xx:00:00:00:00:00", KERNEL=="eth*", NAME="eth0"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="xx:00:00:00:00:00", KERNEL=="wlan*", NAME="wlan0"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="xx:00:00:00:00:00", KERNEL=="wlan*", NAME="wlan1"

发射热点脚本/usr/local/bin/wifi-ap

#!/bin/bash
#===============================================================================
#
#          FILE: wifi-ap
# 
#         USAGE: ./wifi-ap 
# 
#   DESCRIPTION: 
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: Yehun (), yehunhk@163.com
#  ORGANIZATION: Yehun
#       CREATED: 03/04/2018 01:53:05 AM
#      REVISION:  ---
#===============================================================================

ETH0="eth0"
WLAN0="wlan0"
WLAN1="wlan1"
if ifconfig | grep $WLAN1 > /dev/null ;then
    ifconfig $WLAN1 down
    ifconfig $WLAN1 10.10.10.1 netmask 255.255.255.0 up
    #iwconfig $WLAN1 power off
    hostapd -B /etc/hostapd/hostapd.conf
    #service hostapd start
    if ifconfig | grep $WLAN0 > /dev/null ;then
        if [ "down" == `cat /sys/class/net/$WLAN0/operstate` ]; then
            wpa_supplicant -B -i $WLAN0 -c /etc/wpa_supplicant/wpa_supplicant.conf
        fi
        if [ "up" == `cat /sys/class/net/$WLAN0/operstate` ]; then
            echo "start setting wlan0 iptables"
            sh -c "echo 1 >/proc/sys/net/ipv4/ip_forward"
            iptables -t nat -APOSTROUTING -o $WLAN0 -j MASQUERADE
            iptables -A FORWARD -i $WLAN0 -o $WLAN1 -m state --state RELATED,ESTABLISHED -j ACCEPT
            iptables -A FORWARD -i $WLAN1 -o $WLAN0 -j ACCEPT
        elif [ "up" == `cat /sys/class/net/$ETH0/operstate` ]; then
            echo "start setting eth0 iptables"
            sh -c "echo 1 >/proc/sys/net/ipv4/ip_forward"
            iptables -t nat -APOSTROUTING -o $ETH0 -j MASQUERADE
            iptables -A FORWARD -i $ETH0 -o $WLAN1 -m state --state RELATED,ESTABLISHED -j ACCEPT
            iptables -A FORWARD -i $WLAN1 -o $ETH0 -j ACCEPT
        fi
    fi
fi

把wifi-ap加入rc.local开机启动

#!/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

/usr/local/bin/wifi-ap
exit 0

 

转载于:https://my.oschina.net/yehun/blog/1633488

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值