wpa_add script

I have this script named wpa_add, which I use to easily add new WiFi when I am outside, possibly in a cafe. I have written this script because I don’t like the way my girlfriend looks at me while thinking that I am an absolute moron for not using Windows 10, and the entirety of Linux is a circlejerk. It is only natural that she thinks this way. I use my own distribution that doesn’t have things like dbus, or NetworkManager, or one of those common desktop environments. You could install it by creating a simple package, but I am happy to not have any of those in my system.

This script uses wpa-supplicant to add a new network and reconfigure. It uses dmenu for input, however you could replace dmenu calls with some command line prompts. I am doing the following assumptions: - You can manipulate wpa_supplicant without root access. - The configuration is on /etc/wpa_supplicant.conf. - You can edit /etc/wpa/supplicant.conf.

If you want to ensure the above just do the following (as root):

# Add yourself to the wheel group if you aren't already.
adduser user wheel

# Change the ownership of /etc/wpa_supplicant.conf
chown root:wheel /etc/wpa_supplicant.conf

# Make sure the configuration can be edited by the wheel group.
chmod 664 /etc/wpa_supplicant.conf

Your wpa_supplicant configuration must include the following line (or something similar):

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel

Here is the script

#!/bin/sh
# Script to add wpa_supplicant networks through dmenu

if [ "$1" ]; then
    name=$1
else
    name=$(dmenu -p "Please enter network name, leave empty if you want to search" <&-)
fi

[ "$name" ] || {
    wpa_cli scan
    name=$(
    wpa_cli scan_results | sed 1,2d | while read -r _ _ _ _ ssid _; do
        # Hidden wifi are not to be returned
        [ "$ssid" ] || continue
        echo "$ssid"
    done | sort -u | dmenu -l 10 -p "Please choose WiFi")
    [ "$name" ] || exit 1
}

pass=$(dmenu -P -p "Please enter your password, leave empty if the network has open access.")

if [ "$pass" ]; then
    wpa_passphrase "$name" <<EOF>> /etc/wpa_supplicant.conf
$pass
EOF
else
    printf 'network={\n\tssid="%s"\n\tkey_mgmt=NONE\n\tpriority=-999\n}\n' "$name" >> /etc/wpa_supplicant.conf
fi

wpa_cli reconfigure

As I have said, you could do something similar in a command-line-only tool as well. This one uses fzf on WiFi selection.

#!/bin/sh -e

stty="$(stty -g)"
trap "stty $stty" EXIT INT TERM HUP

if [ "$1" ]; then
    name=$1
else
    printf 'Network Name, leave empty if you want to search: '
    read -r name
fi

[ "$name" ] || {
    wpa_cli scan >/dev/null
    name=$(
    wpa_cli scan_results | sed 1,2d | while read -r _ _ _ _ ssid _; do
        # Hidden wifi are not to be returned
        [ "$ssid" ] || continue
        echo "$ssid"
    done | sort -u | fzf --prompt "Please choose WiFi: ")
}

[ "$name" ] || exit 1

stty -echo
printf 'Please enter your password, leave empty if the network has open access.\nPassword: '
read -r pass

if [ "$pass" ]; then
    wpa_passphrase "$name" <<EOF>> /etc/wpa_supplicant.conf
$pass
EOF
else
    printf 'network={\n\tssid="%s"\n\tkey_mgmt=NONE\n\tpriority=-999\n}\n' "$name" >> /etc/wpa_supplicant.conf
fi

wpa_cli reconfigure

These scripts can be found as a gist here

This page in plain-text