cli: Make IP address retrieval on firstlogin run in the background

Otherwise we have to wait until the while loop finishes.
Also includes some fixes and comments.
This commit is contained in:
ColorfulRhino
2024-06-19 16:17:00 +02:00
committed by Igor
parent d1c6fa6e76
commit e6fc6d0990

View File

@@ -488,21 +488,54 @@ 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 server of https://quad9.net/
# IP of the QUAD9 DNS resolver https://quad9.net/
PingServerAddress="9.9.9.9"
pingcount=11
ping -c 1 ${PingServerAddress} > /dev/null 2>&1
while [ $? -ne 0 -o ${pingcount} -gt 0 ]; do
unset RETRY
pingcount=$(( pingcount - 1 ))
GET_IP=$(ip -4 addr | grep 'state UP' -A2 | tail -n2 | head -n1 | awk '{print $2}' | cut -f1 -d'/')
[[ -z "$GET_IP" ]] && RETRY="\e[1m\e[97mwaiting for connection\x1B[0m! Retrying (${pingcount})"
[[ $pingcount -eq 0 ]] && RETRY="\e[1m\e[31mnetwork connecting timeout\x1B[0m!"
echo -e "\e[1A\e[KIP address: \x1B[92m${GET_IP}\x1B[0m${RETRY}"
[[ -n "$GET_IP" ]] && break
sleep 1
ping -c 1 $PingServerAddress > /dev/null 2>&1
done
# 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 &
echo "" # empty line
trap '' 2