创建一个连接到 wifi 的脚本,如果失败则回退到 ad-hoc 网络

创建一个连接到 wifi 的脚本,如果失败则回退到 ad-hoc 网络

我想创建一个脚本,尝试将我连接到 wpa_supplicant conf 指定的 wifi,如果失败,则创建一个临时网络。但我正在为此挣扎。

这是我现在所做的:

#!/bin/bash

# create adhoc network function
createAdHocNetwork(){
    echo "Creating ad-hoc network"
    ifconfig wlan0 down
    iwconfig wlan0 mode ad-hoc
    iwconfig wlan0 essid AccessPoint
    ifconfig wlan0 11.0.0.1 netmask 255.255.255.0 up
    echo "Ad-hoc network created"
}

# connect to wifi function
connect(){
    echo "Trying to connect to configured wifi"
    ifdown wlan0
    wpa_supplicant -B -i wlan0 -c /home/pi/pics/wpa_supplicant.conf
    ifup wlan0
    echo "Wifi configured"
}

echo "================================="
echo "Wifi setup"
echo "================================="
connect
echo "Checking connectivity..."
sleep 5s
ping -c 5 -I wlan0 google.com > /dev/null 2>&1
PINGSTATUS=$?
echo "Status"
echo $PINGSTATUS
if [ $PINGSTATUS -eq 0 ];
    then
        echo "Connected to WiFi"
    else
        echo "Wifi not connected, fallback to ad-hoc"
        createAdHocNetwork
fi

exit 0

看起来它可以连接到配置的 wifi,但不能用于 ad-hoc。

这是我的/etc/network/interfaces

# start interfaces upon start of the system
auto lo wlan0

# register loopback interface
iface lo inet loopback

# use dhcp and allow interface to be started when kernel detects a hotplug event
allow-hotplug eth0
iface eth0 inet dhcp

# use manual ip configuration for wlan0 interface and allow hotplug as well
allow-hotplug wlan0
iface wlan0 inet dhcp

和我的/etc/dhcp/dhcpd.conf

ddns-update-style interim;
default-lease-time 600;
max-lease-time 7200;
authoritative;
log-facility local7;
subnet 11.0.0.0 netmask 255.255.255.0 {
  range 11.0.0.2 11.0.0.150;
}

我可以从其他计算机看到临时网络,但无法连接到它,知道吗?

问候

相关内容