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

116 lines
3.7 KiB
Text
Raw Normal View History

2020-04-22 20:59:50 +00:00
#!/bin/bash
2020-04-22 00:22:35 +00:00
IFACE={{ discovered_wireless_iface }}
2020-05-04 13:58:37 +00:00
RASPBIAN=0
2020-04-27 15:34:43 +00:00
NETPLAN=0
2020-05-04 19:42:10 +00:00
SSID="NA"
2020-05-04 13:58:37 +00:00
# when we get here br0 should be available and dbus wpa_supplicant was started if enabled. None
# of the backends that use wpa_supplicant should be active yet based on the Before= After= lines
# in the iiab-wifi-test.service unit file.
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"
2020-04-27 15:34:43 +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
2020-04-22 00:22:35 +00:00
if [ -f /etc/wpa_supplicant/wpa_supplicant.conf ]; then
echo "RasPiOS"
2020-05-04 13:58:37 +00:00
RASPBIAN=1
2020-05-12 15:45:35 +00:00
if /usr/sbin/rfkill list wifi | grep -q "Soft blocked: yes" ; then
echo "unblocking WiFi"
rfkill unblock wifi
fi
2020-04-22 20:59:50 +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"
2020-04-27 15:34:43 +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
2020-04-27 15:34:43 +00:00
# IIAB hint for NetworkManager
2020-05-04 13:58:37 +00:00
# could scrape /etc/NetworkManager/system-connections/ looking for ssid
2020-04-22 00:22:35 +00:00
if [ -f /etc/iiab/iiab.env ]; then
source /etc/iiab/iiab.env
if [ ! -z $CLIENT_SSID ]; then
SSID=$CLIENT_SSID
fi
fi
2023-04-20 15:02:34 +00:00
# NetworkManager
if [ $SSID == "NA" ] && [ ! -z $(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-23 06:59:52 +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
2020-04-22 20:59:50 +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"
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-23 06:59:52 +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
CHAN=$(($FREQ2 - 2407 ))
2020-04-22 20:59:50 +00:00
CHAN=$(($CHAN / 5 ))
2020-04-22 00:22:35 +00:00
echo "channel is $CHAN for $SSID"
2020-05-04 13:58:37 +00:00
HOSTAPD=`grep channel /etc/hostapd/hostapd.conf | awk -F = '{print $2}'`
echo "Hostapd set for $HOSTAPD"
if [ $CHAN -ne $HOSTAPD ]; then
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