Steeph's Web Site

Go To Navigation
Show/Hide Navigation

Entries tagged 'cat:WiFi'

Bash script to quickly open and close a wifi hotspot

I needed a wifi hotspot to test some phones. So I looked up how to create one quickly and learned about nmcli. It wasn't quick and easy enough for my taste. So here is how I do it from now on.

The Bash script maintains only one connection (named quick-hotspot). You can't create multiple connections or access points with it. Not intentionally, but also not accidentally because you forgot that you still have a hotspot enabled from the last time you needed one. (I know I would at some point.)

Examples:


hotspot # Creates and enables a hotspot with a random name and a random 8-digit password.
hotspot down # Disables the hotspot if it was enabled.
hotspot ⊿ⴼͳΞⵖⵡ∃ doctorwho # Creates and enables a hotspot with the SSID "⊿ⴼͳΞⵖⵡ∃" and the password "doctorwho".
hotspot . password # My use case: quick and short and I don't want to type a complicated password on the phones. SSID: ".", PW: "password"
hotspot down # Deactivates the hotspot regardless of the SSID you used.

If you activate a hotspot while one is already active, it just changes the SSID and password so that no two connections created by this script are ever active at the same time. I'm sure there already is a program that solves this better. But it was fun for me to create and I felt like sharing.

Click here to download the bash script.

Content:

#!/bin/bash

# Quickly create and activate a WiFi hotspot - optionally defining SSID and password - and deactivate it again when you don't need it anymore.

usage() {
  echo "Usage: $(basename $0) ssid passphrase"
  echo "       $(basename $0) down"
  echo "       $(basename $0)"
  exit 1
}

activate() {
  # If the connection "quick-hotspot" doesn't already exist...
  if ! nmcli connection show | grep quick-hotspot; then
    # ... then create it.
    nmcli connection add type wifi ifname '*' con-name quick-hotspot autoconnect no ssid "$ssid"
  else
    # ... else change the SSID of the existing connection.
    nmcli connection modify quick-hotspot ssid "$ssid"
  fi
  # Make the connection an access point
  nmcli connection modify quick-hotspot 802-11-wireless.mode ap 802-11-wireless.band bg ipv4.method shared
  # Change the password
  nmcli connection modify quick-hotspot 802-11-wireless-security.key-mgmt wpa-psk 802-11-wireless-security.psk "$pw"
  # Enable the connection
  nmcli connection up quick-hotspot
}

case $# in
  0)
    ssid=$(</dev/urandom tr -dc A-Z] | head -c8; echo "")
    echo "SSID: $ssid"
    pw=$(shuf -i 10000000-99999999 -n 1) 
    echo "PW: $pw"
    activate
    ;;
  1)
    if [ $1 == down ]; then
      nmcli connection down "quick-hotspot"
      exit 0
    else usage
    fi
    ;;
  2)
    ssid="$1"
    if ! [ ${#2} -ge 8 ]; then
      echo "$(basename $0): Password needs to be 8 characters or longer."
      exit 1
    else
      pw="$2"
      activate
    fi
    ;;
  *)
    usage
    ;;
esac

See also: My Reddit Post