AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / ubuntu / Perguntas / 1473556
Accepted
Rick T
Rick T
Asked: 2023-06-21 02:05:02 +0800 CST2023-06-21 02:05:02 +0800 CST 2023-06-21 02:05:02 +0800 CST

Script para alternar entre redes no ubuntu usando nmcli

  • 772

Estou tentando alternar entre redes já preenchidas usando um arquivo bash usando nmcli , mas recebo dados sem sentido.

Mas entenda bobagens

1) 0
#? 0

Código abaixo.

#!/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!"

Eu corro o script ./network_switch.sh

Mas entenda bobagens

1) 0
#? 0
networking
  • 1 1 respostas
  • 61 Views

1 respostas

  • Voted
  1. Best Answer
    steeldriver
    2023-06-21T02:28:08+08:002023-06-21T02:28:08+08:00

    Em casos como este, é útil usar o declarebuiltin para examinar o resultado da atribuição:

    $ 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")
    

    Você verá que criou um array indexado-a ( ) com um único elemento 0. Isso ocorre porque a name=(...)sintaxe cria matrizes indexadas por padrão e simplesmente avalia as "chaves" com valor de string como 0 numérico e sobrescreve sucessivamente o valor.

    Para criar um array associativo , você precisa declará-lo explicitamente, ou

    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"
    )
    

    ou

    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"
    )
    

    que você pode então verificar com 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" )
    
    • 3

relate perguntas

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    Existe um comando para listar todos os usuários? Também para adicionar, excluir, modificar usuários, no terminal?

    • 9 respostas
  • Marko Smith

    Como excluir um diretório não vazio no Terminal?

    • 4 respostas
  • Marko Smith

    Como descompactar um arquivo zip do Terminal?

    • 9 respostas
  • Marko Smith

    Como instalo um arquivo .deb por meio da linha de comando?

    • 11 respostas
  • Marko Smith

    Como instalo um arquivo .tar.gz (ou .tar.bz2)?

    • 14 respostas
  • Marko Smith

    Como listar todos os pacotes instalados

    • 24 respostas
  • Martin Hope
    Flimm Como posso usar o docker sem sudo? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    led-Zepp Como faço para salvar a saída do terminal em um arquivo? 2014-02-15 11:49:07 +0800 CST
  • Martin Hope
    ubuntu-nerd Como descompactar um arquivo zip do Terminal? 2011-12-11 20:37:54 +0800 CST
  • Martin Hope
    TheXed Como instalo um arquivo .deb por meio da linha de comando? 2011-05-07 09:40:28 +0800 CST
  • Martin Hope
    Ivan Como listar todos os pacotes instalados 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    David Barry Como determino o tamanho total de um diretório (pasta) na linha de comando? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher "Os seguintes pacotes foram retidos:" Por que e como resolvo isso? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford Como os PPAs podem ser removidos? 2010-07-30 01:09:42 +0800 CST

Hot tag

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve