MANAGING WIRELESS CONNECTIONS FROM SHELL

It should do the trick, either connecting to a known or new wifi AP. "Known" AP details should be stored in the /etc/wpa_supplicant/whatever.conf path:
 


#!/bin/bash

# Dependencies:
# - wpa_supplicant
# - dhcpcd
# - sudo
# - systemd

WIRELESS_INTERFACE='wlan0' # Change this accordingly

function connect() {
  sudo ip link set dev ${WIRELESS_INTERFACE} up && \
  sudo ip addr add ${IPADDR} dev ${WIRELESS_INTERFACE} && \
  sudo ip ro add default via ${GATEWAY} onlink dev ${WIRELESS_INTERFACE} || echo "Error."
}

function wifi_known() {
  sudo wpa_supplicant -B -i ${WIRELESS_INTERFACE} -c /etc/wpa_supplicant/${SSID}.conf &>/dev/null || echo "Error."
}

function wifi_new() {
  sudo wpa_supplicant -B -i ${WIRELESS_INTERFACE} -c <(wpa_passphrase ${SSID} ${PSK}) &>/dev/null || echo "Error."
}

function disconnect() {
  sudo ip addr flush dev ${WIRELESS_INTERFACE}
  sudo ip route flush dev ${WIRELESS_INTERFACE}
  sudo ip link set ${WIRELESS_INTERFACE} down

  sudo wpa_cli disconnect &>/dev/null
  sudo pkill wpa_supplicant
  sudo systemctl stop [email protected]${WIRELESS_INTERFACE}

  if [ $(echo ${CURRENT_AP} |wc -w) -ne 1 ]; then
          echo -e "\nCurrently no connected, could not disconnect.\n"
  fi
}

function pick() {
  echo -e "\nSelect one network: "
  select SSID in $(ls /etc/wpa_supplicant/ |awk -F '.' '{ print $1 }' |sed 's/.*/\u&/'); do

  case ${SSID} in
      Example_network_SSID) # Put your connection details for any known AP here:
        IPADDR='192.168.1.100/24'
        GATEWAY='192.168.1.1'
        SSID='Example_network_SSID'
        echo -e "\nConnecting to SSID \"${SSID}\""
        connect && \
        wifi_known || exit 1
      ;;
      *) echo "Unknown AP connection details or invalid option. Exiting." ; exit 1
      ;;
  esac

  exit
  done
}

function new_connection() {
read -p "Provide network details:

- SSID: " NEW_SSID
read -sp "- WPA/PSK: " PSK
echo
read -p "- Static (1) or dynamic address (2): " CONN_TYPE

  case ${CONN_TYPE} in
          1) read -p "IP address in CIDR format (IP.ADD.DRE.SS/NETMASK): " IPADDR
             read -p "Gateway: " GATEWAY
             connect && \
             wifi_new || exit 1
          ;;
          2) sudo su -c "wpa_supplicant -B -i ${WIRELESS_INTERFACE} -c <(wpa_passphrase ${NEW_SSID} ${PSK})" && \
             sudo systemctl start [email protected]${WIRELESS_INTERFACE} || exit 1
          ;;
          *) echo -e "\nInvalid option, type '1' or '2'. Exiting." ; exit 1
          ;;
  esac
}

CURRENT_AP=$(sudo wpa_cli list_networks 2>/dev/null |tail -1 |awk -F' ' '{ print $2 }')

if [ $(echo ${CURRENT_AP} |wc -w) -eq 1 ]; then
        echo -e "\n====================================\n Currently connected to: ${CURRENT_AP}\n====================================\n"
else
        echo -e "\n === Currently NOT connected ===\n"
fi

read -p "Select an option:

1) Connect to a known wifi AP
2) Connect to a new wifi AP
3) Disconnect from current AP
q) Exit

Your choice:
#? " OPTION

case ${OPTION} in
        1) disconnect &>/dev/null ; pick || exit 1
        ;;
        2) echo ; new_connection || exit 1
        ;;
        3) disconnect || exit 1
        ;;
        q) exit
        ;;
        *) echo -e "\nInvalid option. Exiting.\n" ; exit 1
        ;;
esac