mirror of
https://github.com/armbian/build
synced 2025-09-24 19:47:06 +07:00
cli: firstlogin: Fix and improve IP address retrieval
This should also include the case if zero retries are left, but the IP address has been found on this last retry.
This commit is contained in:
@@ -141,36 +141,34 @@ 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
|
||||
# How many seconds to wait at maximum to find out the local IP address
|
||||
local ip_wait_seconds_counter=6
|
||||
local local_device_ip=""
|
||||
local retry_message=""
|
||||
|
||||
# First ping
|
||||
ping -c 1 ${PingServerAddress} > /dev/null 2>&1
|
||||
while [[ -z "$local_device_ip" ]] && [ ${ip_wait_seconds_counter} -ge 0 ]; do
|
||||
local_device_ip=$(ip -4 addr show scope global | grep inet | awk '{print $2}' | cut -d/ -f1)
|
||||
|
||||
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!"
|
||||
# Set retry message if some retries are left, but no IP address has been found yet
|
||||
if [[ -z "$local_device_ip" ]] && [[ $ip_wait_seconds_counter -gt 0 ]]; then
|
||||
retry_message="\e[1m\e[97mWaiting for local connection!\x1B[0m Retrying... (${ip_wait_seconds_counter})"
|
||||
# Empty retry message if IP address has been found
|
||||
elif [[ -n "$local_device_ip" ]]; then
|
||||
retry_message=""
|
||||
# Set timeout message if no retries are left and no IP address has been found
|
||||
else
|
||||
retry_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}"
|
||||
echo -e "\e[1A\e[KIP address: \x1B[92m${local_device_ip}\x1B[0m ${retry_message}"
|
||||
|
||||
[[ -n "$GET_IP" ]] && break
|
||||
# Wait for 1 second if the IP has not yet been found
|
||||
if [[ -z "$local_device_ip" ]]; then
|
||||
sleep 1
|
||||
fi
|
||||
|
||||
# Wait for 1 second and ping the server address again
|
||||
sleep 1
|
||||
ping -c 1 $PingServerAddress > /dev/null 2>&1
|
||||
ip_wait_seconds_counter=$((ip_wait_seconds_counter - 1))
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user