1
0
Fork 0
mirror of https://github.com/iiab/iiab.git synced 2025-02-15 04:32:11 +00:00
iiab/roles/network/templates/hostapd/iiab-test-wifi.j2

104 lines
3.4 KiB
Text
Raw Normal View History

#!/bin/sh
2020-04-22 00:22:35 +00:00
IFACE={{ discovered_wireless_iface }}
2020-04-27 15:34:43 +00:00
NETPLAN=0
2023-04-23 17:23:33 +00:00
SSID=""
# when we get here br0 should be available and dbus wpa_supplicant was started if enabled. Some
# of the backends that use wpa_supplicant should be active based on the Before= After= lines in
# the iiab-wifi-test.service unit file.
echo "iiab-test-wifi called"
echo "running pid $$"
2020-05-04 13:58:37 +00:00
2020-04-27 15:34:43 +00:00
# covers systemd-networkd
if [ -f /etc/wpa_supplicant/wpa_supplicant-$IFACE.conf ]; then
echo "systemd-network"
2023-04-23 07:03:16 +00:00
SSID=$(grep ssid /etc/wpa_supplicant/wpa_supplicant-$IFACE.conf | awk -F = '{print $2}' | sed -r s/\"// | sed -r s/\"//)
2020-04-22 00:22:35 +00:00
fi
# covers stock raspbian
2023-04-24 06:38:56 +00:00
if [ -f /etc/wpa_supplicant/wpa_supplicant.conf ] && [ -n "$(pgrep dhcpcd)" ]; then
echo "RasPiOS"
2023-04-23 07:03:16 +00:00
SSID=$(grep ssid /etc/wpa_supplicant/wpa_supplicant.conf | awk -F = '{print $2}' | sed -r s/\"// | sed -r s/\"//)
2020-04-22 00:22:35 +00:00
fi
2020-04-27 15:34:43 +00:00
# https://bugs.launchpad.net/ubuntu/+source/linux-firmware/+bug/1862760
# https://bugs.launchpad.net/netplan/+bug/1951586
# WiFi country code progress on arm64 OS's discussed on #3078
2023-04-20 15:02:34 +00:00
# covers netplan systemd use on server with bug workarounds
2023-04-18 21:08:08 +00:00
if [ -f /run/netplan/wpa-$IFACE.conf ]; then
2023-04-19 15:46:45 +00:00
NETPLAN=1
echo "Netplan systemd"
2023-04-23 07:03:16 +00:00
SSID=$(grep ssid /run/netplan/wpa-$IFACE.conf | awk -F = '{print $2}' | sed -r s/\"// | sed -r s/\"//)
2023-04-18 21:08:08 +00:00
REG_DOM=$(grep country /run/netplan/wpa-$IFACE.conf | awk -F = '{ print $2 }')
if [ -z "$REG_DOM" ]; then
echo "cover netplan wifi client lack of country= setting to {{ host_country_code }}"
sed -i "s|ctrl_interface=/run/wpa_supplicant|&\ncountry={{ host_country_code }}|" /run/netplan/wpa-$IFACE.conf
2023-04-18 21:08:08 +00:00
else
echo "set hostapd wifi country to $REG_DOM"
if [ -f /etc/hostapd/hostapd.conf.iiab ]; then
sed -i "s|^country.*|country_code=$REG_DOM|" /etc/hostapd/hostapd.conf.iiab
2023-04-18 21:08:08 +00:00
cp /etc/hostapd/hostapd.conf.iiab /etc/hostapd/hostapd.conf
fi
fi
2020-04-27 15:34:43 +00:00
fi
2023-04-18 21:08:08 +00:00
2023-04-20 15:02:34 +00:00
# NetworkManager
2023-04-24 06:38:56 +00:00
if [ -z "$SSID" ] && [ -n "$(pgrep NetworkManager)" ]; then
echo "NetworkManager"
sleep 15
2023-04-20 15:02:34 +00:00
SSID=$(iw $IFACE info | grep ssid | awk '{print $2}' )
fi
2020-05-04 19:42:10 +00:00
echo "ssid is $SSID"
2023-04-20 15:02:34 +00:00
2023-04-24 06:38:56 +00:00
if [ -z "$SSID" ]; then
echo "Couldn't find ssid $SSID to use exiting"
if [ $NETPLAN -eq 1 ]; then
echo "Netplan1"
fi
2020-05-04 19:42:10 +00:00
exit 0
fi
2020-04-27 14:44:17 +00:00
wpa_cli -i $IFACE scan > /dev/null
2020-05-05 16:27:57 +00:00
sleep 2
2023-04-24 06:38:56 +00:00
FREQ=$(wpa_cli -i $IFACE scan_results | grep "$SSID" | awk '{print $2}')
for result in $FREQ; do
2020-04-27 14:44:17 +00:00
echo "frequency is $result for $SSID"
2023-04-24 06:38:56 +00:00
if [ "$result" -lt 2485 ] && [ "$result" -gt 2407 ]; then
FREQ2=$result
break
2020-09-08 00:08:51 +00:00
else
echo "channel $result is 5Ghz - ignoring"
fi
done
echo "Using $FREQ2 for $SSID"
if [ $NETPLAN -eq 1 ]; then
echo "Netplan2"
# This is more of a netplan workaround should go away.
/bin/systemctl restart netplan-wpa-$IFACE.service
fi
2023-04-24 06:38:56 +00:00
if [ -z "$FREQ2" ]; then
echo "Couldn't find frequency to use exiting"
if [ $NETPLAN -eq 1 ]; then
echo "Netplan3"
fi
exit 0
fi
# ubuntu on boot exits at this point timing - issue with wpa_cli and scanning
2023-04-24 06:38:56 +00:00
CHAN=$((FREQ2 - 2407 ))
CHAN=$((CHAN / 5 ))
2020-04-22 00:22:35 +00:00
echo "channel is $CHAN for $SSID"
2023-04-23 07:03:16 +00:00
HOSTAPD=$(grep channel /etc/hostapd/hostapd.conf | awk -F = '{print $2}')
2020-05-04 13:58:37 +00:00
echo "Hostapd set for $HOSTAPD"
2023-04-24 06:38:56 +00:00
if [ "$CHAN" -ne "$HOSTAPD" ]; then
2020-05-04 13:58:37 +00:00
echo "Editing Hostapd for channel $CHAN"
cp /etc/hostapd/hostapd.conf.iiab /etc/hostapd/hostapd.conf
sed -i -e "s/^channel.*/channel=$CHAN/" /etc/hostapd/hostapd.conf
/bin/systemctl --no-block restart hostapd
2023-04-23 06:59:52 +00:00
echo "Restarted hostapd"
2020-04-27 15:34:43 +00:00
fi
2020-04-27 14:44:17 +00:00
exit 0