1

How to write shell script that will automate interactive steps to connect to wifi via terminal?

Steps outlined in How do I connect to a WPA wifi network using the command line? with further details:

  1. run script, ask to enter ssid
  2. ssid enter, ask to enter passphrase
  3. psk hash returned, generate ssid + hash output to wpa_supplicant.conf
  4. set the interface to listen on and run on background
  5. connect to network
  6. ifconfig wlan0 to show status
  7. ask to enter nameservers, script write to /etc/resolv.conf
  8. issue ping to a domain and issue ctrl-c to stop it after a minute or so
  • 3
    why write a script yourself ? Use wicd-cli or similar – Panther Mar 27 '15 at 22:47
  • 1
    um . . . are you aware there's also nmcli ? very convenient for using in terminal , you know. just read through the man page for more info. – Sergiy Kolodyazhnyy Mar 28 '15 at 1:57
  • I'm not familiar with any of these, can you please show me an example? The purpose is for it to be used by non-linux person, so it cannot be manual command tool. The system has no desktop interface. – Sean Lee Mar 28 '15 at 5:40
  • Here you're a example. – 0x2b3bfa0 Mar 28 '15 at 13:28
3

I've made this:

#!/bin/bash

## Restores the screen when the program exits.
trap "tput rmcup; exit"  SIGHUP SIGINT SIGTERM

## Saves the screen contents.
tput smcup

## Clears the screen.
clear

## Loop through available interfaces.
while read interface; do                    # While reads a line of the output
    i=$((i+1))                                  # Only God knows what does this (view note 1).
    type=$(cut -f2 -d ' ' <<< $interface)       # Saves the interface type to check if is wifi.
    status=$(cut -f3 -d ' ' <<< $interface)     # Saves the status of the current interface.
    interface=$(cut -f1 -d ' ' <<< $interface)  # Selects the INTEFACE field of the output.
    if [[ "$type" == "802-11-wireless" ]]; then # If is a WiFi interface then:
      interfaces[$i]=$interface                     # Adds the current interface to an array.
      echo "$i: $interface ($status)"               # Prints the name of current interface.
    fi                                          # Ends the if conditional
done < <(nmcli device | tail -n +2)         # Redirects the output of the command nmcli device to the loop.

## If there is only one interface
if [[ "$i" == "2" ]]; then
    iface=1 # Selected interface is the only one
    clear   # Quick and dirty workaround for make disappear the interface list.
else
    ## Prompts the user for the interface to use.
    read -p "Select the interface: " iface
fi

## If the entered number is valid then...
if [[ "$iface" -le $i ]]; then
    read -p "Enter the SSID or BSSID: " b_ssid # Prompts the user for the ESSID/BSSID
    read -p "Enter the password: " pass # Prompts the user for the password
    output=$(nmcli device wifi connect "$b_ssid" password "$pass" iface wlan0 --timeout 10) # Tries to connect
    wget -q --tries=5 --timeout=5 --spider http://google.com &> /dev/null # Is connected to Internet?
    if [[ $? -eq 0 ]]; then
            echo "You're connected." # Is connected to Internet
            exit 0
    else
            echo "Error. $output" # Anything goes wrong
            exit 1
    fi
else
    echo "Invalid interface entered. Exiting..."
    exit 2
fi

## Note 1: this line increments $i
  • Thank you for the example, but as written in my comment above, there is no desktop interface on the system. nmcli required glib, and not the way to go. It was my bad to not clarify that I need example on the shell script, not nmcli. – Sean Lee Mar 30 '15 at 7:21
  • Ok, then, what utility would you use? Is wpa_cli installed? – 0x2b3bfa0 Mar 30 '15 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.