website

My personal website
git clone git://git.ckyln.com/website
Log | Files | Refs

20200828-wpa-add-script.txt (3456B)


      1 wpa_add script
      2 ================================================================================
      3 
      4 I have this script named `wpa_add`, which I use to easily add new WiFi when I
      5 am outside, possibly in a cafe. I have written this script because I don't like
      6 the way my girlfriend looks at me while thinking that I am an absolute moron for
      7 not using Windows 10, and the entirety of Linux is a circlejerk. It is only
      8 natural that she thinks this way. I use my own distribution that doesn't have
      9 things like `dbus`, or `NetworkManager`, or one of those common desktop
     10 environments. You could install it by creating a simple package, but I am happy
     11 to not have any of those in my system.
     12 
     13 This script uses wpa-supplicant to add a new network and reconfigure. It uses
     14 dmenu for input, however you could replace dmenu calls with some command line
     15 prompts. I am doing the following assumptions:
     16 - You can manipulate `wpa_supplicant` without root access.
     17 - The configuration is on `/etc/wpa_supplicant.conf`.
     18 - You can edit `/etc/wpa/supplicant.conf`.
     19 
     20 If you want to ensure the above just do the following (as root):
     21 
     22 ```sh
     23 # Add yourself to the wheel group if you aren't already.
     24 adduser user wheel
     25 
     26 # Change the ownership of /etc/wpa_supplicant.conf
     27 chown root:wheel /etc/wpa_supplicant.conf
     28 
     29 # Make sure the configuration can be edited by the wheel group.
     30 chmod 664 /etc/wpa_supplicant.conf
     31 ```
     32 
     33 Your `wpa_supplicant` configuration must include the following line (or something similar):
     34 
     35 ```plaintext
     36 ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel
     37 ```
     38 
     39 
     40 Here is the script
     41 
     42 ```sh
     43 #!/bin/sh
     44 # Script to add wpa_supplicant networks through dmenu
     45 
     46 if [ "$1" ]; then
     47     name=$1
     48 else
     49     name=$(dmenu -p "Please enter network name, leave empty if you want to search" <&-)
     50 fi
     51 
     52 [ "$name" ] || {
     53     wpa_cli scan
     54     name=$(
     55     wpa_cli scan_results | sed 1,2d | while read -r _ _ _ _ ssid _; do
     56         # Hidden wifi are not to be returned
     57         [ "$ssid" ] || continue
     58         echo "$ssid"
     59     done | sort -u | dmenu -l 10 -p "Please choose WiFi")
     60     [ "$name" ] || exit 1
     61 }
     62 
     63 pass=$(dmenu -P -p "Please enter your password, leave empty if the network has open access.")
     64 
     65 if [ "$pass" ]; then
     66     wpa_passphrase "$name" <<EOF>> /etc/wpa_supplicant.conf
     67 $pass
     68 EOF
     69 else
     70     printf 'network={\n\tssid="%s"\n\tkey_mgmt=NONE\n\tpriority=-999\n}\n' "$name" >> /etc/wpa_supplicant.conf
     71 fi
     72 
     73 wpa_cli reconfigure
     74 ```
     75 
     76 
     77 As I have said, you could do something similar in a command-line-only tool as
     78 well. This one uses `fzf` on WiFi selection.
     79 
     80 ```sh
     81 #!/bin/sh -e
     82 
     83 stty="$(stty -g)"
     84 trap "stty $stty" EXIT INT TERM HUP
     85 
     86 if [ "$1" ]; then
     87     name=$1
     88 else
     89     printf 'Network Name, leave empty if you want to search: '
     90     read -r name
     91 fi
     92 
     93 [ "$name" ] || {
     94     wpa_cli scan >/dev/null
     95     name=$(
     96     wpa_cli scan_results | sed 1,2d | while read -r _ _ _ _ ssid _; do
     97         # Hidden wifi are not to be returned
     98         [ "$ssid" ] || continue
     99         echo "$ssid"
    100     done | sort -u | fzf --prompt "Please choose WiFi: ")
    101 }
    102 
    103 [ "$name" ] || exit 1
    104 
    105 stty -echo
    106 printf 'Please enter your password, leave empty if the network has open access.\nPassword: '
    107 read -r pass
    108 
    109 if [ "$pass" ]; then
    110     wpa_passphrase "$name" <<EOF>> /etc/wpa_supplicant.conf
    111 $pass
    112 EOF
    113 else
    114     printf 'network={\n\tssid="%s"\n\tkey_mgmt=NONE\n\tpriority=-999\n}\n' "$name" >> /etc/wpa_supplicant.conf
    115 fi
    116 
    117 wpa_cli reconfigure
    118 ```
    119 
    120 These scripts can be found as a gist [here](https://git.io/JULL6)