cli: firstlogin: Move IP address retrieval into function and don't run in bg

Trying to run this in the background does not work correctly in its current iteration, setting the correct place on the screen for displaying did not work out.
Includes minor fixes and wording improvements.
This commit is contained in:
ColorfulRhino 2024-06-20 13:01:21 +02:00 committed by Igor
parent a95bdd920f
commit 56267aa0ad

View File

@ -139,6 +139,41 @@ do_firstrun_automated_network_configuration()
fi
} #do_firstrun_automated_network_configuration
# Try to retrieve the local IP address to display
get_local_ip_addr() {
# IP of the QUAD9 DNS resolver https://quad9.net/
PingServerAddress="9.9.9.9"
# How many times to ping before the script can continue (1 ping is about 1 second)
pingcount=6
# First ping
ping -c 1 ${PingServerAddress} > /dev/null 2>&1
while [ $? -ne 0 ] && [ ${pingcount} -gt 0 ]; do
pingcount=$((pingcount - 1))
GET_IP=$(ip -4 addr show scope global | grep inet | awk '{print $2}' | cut -d/ -f1)
# Set retry message if no IP address has been found yet
if [[ -z "$GET_IP" ]]; then
MESSAGE="\e[1m\e[97mWaiting for local connection\x1B[0m! Retrying... (${pingcount})"
fi
# Set timeout message if no retries are left
if [[ $pingcount -eq 0 ]]; then
MESSAGE="\e[1m\e[31mNetwork connection timeout\x1B[0m!"
fi
# Display the message
echo -e "\e[1A\e[KIP address: \x1B[92m${GET_IP}\x1B[0m ${MESSAGE}"
[[ -n "$GET_IP" ]] && break
# Wait for 1 second and ping the server address again
sleep 1
ping -c 1 $PingServerAddress > /dev/null 2>&1
done
}
read_password() {
unset password
@ -510,53 +545,9 @@ if [[ -f /root/.not_logged_in_yet && -n $(tty) ]]; then
echo -e "Documentation: \e[1m\e[92m${VENDORDOCS}\x1B[0m | Community support: \e[1m\e[92m${VENDORSUPPORT}\x1B[0m\n"
echo "" # empty line
# IP of the QUAD9 DNS resolver https://quad9.net/
PingServerAddress="9.9.9.9"
pingcount=11
# Try to retrieve the local IP address to display
retrieve_ip_addr() {
# First ping
ping -c 1 ${PingServerAddress} > /dev/null 2>&1
while [ $? -ne 0 ] && [ ${pingcount} -gt 0 ]; do
pingcount=$((pingcount - 1))
GET_IP=$(ip -4 addr show scope global | grep inet | awk '{print $2}' | cut -d/ -f1)
# Check if the IP address has been found
if [[ -z "$GET_IP" ]]; then
RETRY="\e[1m\e[97mWaiting for connection\x1B[0m! Retrying (${pingcount})"
fi
# Set the retry message
if [[ $pingcount -eq 0 ]]; then
RETRY="\e[1m\e[31mNetwork connection timeout\x1B[0m!"
fi
# Save the current cursor position
echo -ne "\e[s"
# Move the cursor to the fixed IP address row and clear the line
echo -ne "\e[${ip_address_row};0H\e[K"
# Display the retry message
echo -e "\e[1A\e[KIP address: \x1B[92m${GET_IP}\x1B[0m ${RETRY}"
# Restore the cursor position
echo -ne "\e[u"
[[ -n "$GET_IP" ]] && break
# Wait for 1 second and ping the server address again
sleep 1
ping -c 1 $PingServerAddress > /dev/null 2>&1
done
}
# Save the row number after the last output
ip_address_row=$(($(tput lines) - 1))
# Run the IP retrieval function in the background
retrieve_ip_addr &
# Try to get the local IP address (script halts until IP was found or x retries were done)
get_local_ip_addr
echo "" # empty line