Score:2

Script to switch between networks in ubuntu using nmcli

cz flag

I'm trying to switch between already populated networks using a bash file using nmcli but I'm get non sense data back.

But get nonsense

1) 0
#? 0

Code below.

#!/bin/bash

# Pre-populated UUID data for network connections
networks=(
    ["tr_5"]="127f3e9e-34dd-444e-aee4-7c2b35c7c307"
    ["VC_HotSpot_6"]="c498aa6a-24d4-4b51-92c7-9b8d84181fc1"
    ["Network_3"]="34567890-3456-3456-3456-345678901234"
)

# List available network connections
echo "Available network connections:"
nmcli connection show

# Prompt the user to choose a network
echo "Select the network to connect to:"
select network_name in "${!networks[@]}"; do
    if [[ -n $network_name ]]; then
        break
    fi
done

# Get the UUID of the selected network
network_uuid=${networks[$network_name]}

# Check if the network UUID exists
if [[ -z $network_uuid ]]; then
    echo "Network '$network_name' UUID not found. Exiting..."
    exit 1
fi

# Disconnect from the current network (if connected)
current_connection=$(nmcli connection show --active | awk 'NR>1{print $1}')
if [[ -n $current_connection ]]; then
    echo "Disconnecting from the current network..."
    nmcli connection down $current_connection
fi

# Connect to the chosen network
echo "Connecting to network '$network_name'..."
nmcli connection up uuid $network_uuid

# Display the connection status
echo "Connection status:"
nmcli connection show --active | grep -E "($network_name|$current_connection)"

echo "Network switch completed successfully!"

I run the script ./network_switch.sh

But get nonsense

1) 0
#? 0
Score:3
hr flag

In cases like this, it's helpful to use the declare builtin to examine the result of the assignment:

$ networks=(
    ["tr_5"]="127f3e9e-34dd-444e-aee4-7c2b35c7c307"
    ["VC_HotSpot_6"]="c498aa6a-24d4-4b51-92c7-9b8d84181fc1"
    ["Network_3"]="34567890-3456-3456-3456-345678901234"
)

$ declare -p networks
declare -a networks=([0]="34567890-3456-3456-3456-345678901234")

You will see that you have created an indexed array (-a) with a single 0th element. That's because the name=(...) syntax creates indexed arrays by default, and it simply evaluates the string-valued "keys" as numerical 0 and successively overwrites the value.

To create an associative array you need to declare it explicitly, either

declare -A  networks=(
    ["tr_5"]="127f3e9e-34dd-444e-aee4-7c2b35c7c307"
    ["VC_HotSpot_6"]="c498aa6a-24d4-4b51-92c7-9b8d84181fc1"
    ["Network_3"]="34567890-3456-3456-3456-345678901234"
)

or

typeset -A  networks=(
    ["tr_5"]="127f3e9e-34dd-444e-aee4-7c2b35c7c307"
    ["VC_HotSpot_6"]="c498aa6a-24d4-4b51-92c7-9b8d84181fc1"
    ["Network_3"]="34567890-3456-3456-3456-345678901234"
)

which you can then check with declare -p:

$ declare -p networks
declare -A networks=([tr_5]="127f3e9e-34dd-444e-aee4-7c2b35c7c307" [Network_3]="34567890-3456-3456-3456-345678901234" [VC_HotSpot_6]="c498aa6a-24d4-4b51-92c7-9b8d84181fc1" )
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.