Skip to content

Commit

Permalink
Bugifx: addressing two problems at firstlogin script
Browse files Browse the repository at this point in the history
- removing network manager dependancy
- errors when wireless networking is still not detected
  • Loading branch information
igorpecovnik committed Jun 29, 2024
1 parent 8ca4068 commit 4519ded
Showing 1 changed file with 63 additions and 46 deletions.
109 changes: 63 additions & 46 deletions packages/bsp/common/usr/lib/armbian/armbian-firstlogin
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,7 @@ set_timezone_and_locales() {
PUBLIC_IP=$(curl --max-time 5 -s https://ipinfo.io/ip)

# Check if we have wireless adaptor
if command -v nmcli > /dev/null 2>&1; then
WIFI_DEVICE=$(LC_ALL=C nmcli dev status | grep " wifi " 2> /dev/null)
elif command -v iw > /dev/null 2>&1; then
WIFI_DEVICE=$(LC_ALL=C iw dev | awk '$1=="Interface"{print $2}' 2> /dev/null)
fi
WIFI_DEVICE=$(LC_ALL=C iw dev | awk '$1=="Interface"{print $2}' 2> /dev/null)

if [ -z "$PUBLIC_IP" ]; then

Expand All @@ -289,49 +285,70 @@ set_timezone_and_locales() {
if [[ "${response}" =~ ^(Y|y)$ ]]; then

# get list of wireless networks
echo -e "\nDetected wireless networks:\n"
ARRAY=($(sudo iwlist ${WIFI_DEVICE} scanning | egrep 'ESSID' | sed 's/^[ \t]*//' | sed 's/"//g' | sed 's/ESSID://' | awk 'BEGIN{FS=OFS=","} {$NF=++count OFS $NF} 1'))
while [[ 1 ]] ; do
while [[ 1 ]] ; do
for str in ${ARRAY[@]}; do echo $str | sed "s/,/ \t /g"; done
read -r -p "Enter a number of SSID: " input
if [[ "$input" =~ ^[0-9]{,2}$ ]] ; then break; fi
done
echo ""
# get password
while [[ 1 ]] ; do
SSID=$(echo ${ARRAY[$input-1]} | cut -d"," -f2)
read -r -p "Enter a password for ${SSID}: " password
break
done

# generate config
cat <<- EOF > "${SDCARD}"/etc/netplan/30-wifis-dhcp.yaml
# Created by Armbian firstlogin script
network:
wifis:
${WIFI_DEVICE}:
dhcp4: yes
dhcp6: yes
access-points:
"$SSID":
password: "${password}"
EOF

# apply to netplan
systemctl daemon-reload
netplan apply --timeout 0 2>/dev/null
sleep 5

# exit if connection is suffesful
if [[ -n $(sudo iw ${WIFI_DEVICE} link | grep "$SSID") ]]; then break; fi
scanning=0
broken=1
while [[ ${scanning} -lt 3 ]]; do
sleep 0.5
scanning=$(( scanning + 1 ))
ARRAY=($(iwlist ${WIFI_DEVICE} scanning 2> /dev/null | egrep 'ESSID' | sed 's/^[ \t]*//' | sed 's/"//g' | sed 's/ESSID://' | awk 'BEGIN{FS=OFS=","} {$NF=++count OFS $NF} 1'))
if [[ $? == 0 ]]; then broken=0; break; fi
done
# wifi can also fail
if [[ ${broken} == 1 ]]; then
echo -e "\nWireless connection was \x1B[91mnot detected\x1B[0m.\n"
else
echo -e "\nDetected wireless networks:\n"
scanning=0
broken=1
while [[ ${scanning} -lt 3 ]]; do
scanning=$(( scanning + 1 ))
while [[ 1 ]] ; do
for str in ${ARRAY[@]}; do echo $str | sed "s/,/ \t /g"; done
echo ""
read -r -p "Enter a number of SSID: " input
if [[ "$input" =~ ^[0-9]{,2}$ ]] ; then break; fi
done
# get password
while [[ 1 ]] ; do
SSID=$(echo ${ARRAY[$input-1]} | cut -d"," -f2)
echo ""
read -r -p "Enter a password for ${SSID}: " password
break
done

# generate config
cat <<- EOF > "${SDCARD}"/etc/netplan/30-wifis-dhcp.yaml
# Created by Armbian firstlogin script
network:
wifis:
${WIFI_DEVICE}:
dhcp4: yes
dhcp6: yes
access-points:
"$SSID":
password: "${password}"
EOF
chmod 600 /etc/netplan/30-wifis-dhcp.yaml

# apply to netplan
systemctl daemon-reload
netplan apply --timeout 0 2>/dev/null
sleep 5

# exit if connection is suffesful
if [[ -n $(iw "${WIFI_DEVICE}" link 2> /dev/null | grep "$SSID") ]]; then broken=0; break; fi
done
if [[ ${broken} == 1 ]]; then
echo -e "\n\x1B[91mUnable to connect to Access Point\x1B[0m.\n"
fi
fi # detected or not detected wireless network
fi
echo ""
fi
fi

# Grab IP once again if not found
sleep 3
[[ -z "$PUBLIC_IP" && -n "$WIFI_DEVICE" ]] && PUBLIC_IP=$(curl --max-time 5 -s https://ipinfo.io/ip)

# Call the geolocation API and capture the output
Expand All @@ -346,15 +363,17 @@ set_timezone_and_locales() {

TZDATA=$(echo "${RES}" | cut -d"," -f1)
CCODE=$(echo "${RES}" | cut -d"," -f3 | xargs)
echo -e "Detected timezone: \x1B[92m$TZDATA\x1B[0m"
echo ""

unset response
while [[ ! "${response}" =~ ^(Y|y|N|n)$ ]]; do
if [ -z $SET_LANG_BASED_ON_LOCATION ];then
if [ -z "${SET_LANG_BASED_ON_LOCATION}" ] && [ -n "${TZDATA}" ];then
echo -e "Detected timezone: \x1B[92m$TZDATA\x1B[0m"
echo ""
read -r -p "Set user language based on your location? [Y/n] " response
response=${response:-Y}
else
response=$SET_LANG_BASED_ON_LOCATION
break
fi
done
# change it only if we have a match and if we agree
Expand All @@ -367,13 +386,11 @@ set_timezone_and_locales() {
# UTF8 is not present everywhere so check again in case it returns empty value
[[ -z "$LOCALES" ]] && LOCALES=$(grep territory /usr/share/i18n/locales/* | grep _"$CCODE" | cut -d ":" -f 1 | cut -d "/" -f 6 |
xargs -I{} grep {} /usr/share/i18n/SUPPORTED | cut -d " " -f 1)

readarray -t options <<< "${LOCALES}"

if [ -z $PRESET_LOCALE ];then
# when having more locales, prompt for choosing one
if [[ "${#options[@]}" -gt 1 ]]; then

options+=("Skip generating locales")
echo -e "\nAt your location, more locales are possible:\n"
PS3='Please enter your choice:'
Expand Down

0 comments on commit 4519ded

Please sign in to comment.