dotfiles/bin/qrwifi
Midgard b83a91e303
[bin/qrwifi] Add interactive mode
to choose a saved network to share
2023-07-26 18:18:14 +02:00

48 lines
1.4 KiB
Bash
Executable file

#!/bin/sh
DMENU="$(which dmenu 2>/dev/null)"
if [ -z "$DMENU" ]; then
DMENU="$(which rofi) -dmenu"
fi
if [ "${1:-}" = -i ] || [ "${1:-}" = --interactive ]; then
ssid="$(sudo cat /etc/wpa_supplicant/wpa_supplicant.conf | sed -n '/^[\t ]*ssid="[^"][^"]*"$/{:findpsk;N;/}/d;/\n[\t ]*psk=/!b findpsk;s/^\(\n\|.\)*[\t ]*ssid="\([^"]*\)"\(\n\|.\)*$/\2/p; }' | $DMENU)"
if [ -z "$ssid" ]; then
exit 1
fi
else
ssid="${1:-}"
fi
if [ -z "$ssid" ]; then
ssid="$(wpa_cli status | rg -o --replace='$1' '^ssid=(.*)$')"
fi
if [ -z "$ssid" ]; then
echo "Not connected to a WiFi network via wpa_supplicant"
echo "Use $0 <ssid> [password]" to share another WiFi network
exit 1
fi
pw="${2:-}"
if [ -z "$pw" ]; then
pw="$(sudo cat /etc/wpa_supplicant/wpa_supplicant.conf | sed -n '/^[\t ]*ssid="'"$ssid"'"$/{:findpsk;N;/}/d;/\n[\t ]*psk=/!b findpsk;s/^\(\n\|.\)*[\t ]*psk="\([^"]*\)"$/\2/p; t quit}; d; :quit; q')"
fi
if [ -z "$pw" ]; then
echo "Couldn't find password for network \`$ssid\`"
echo "Use $0 <ssid> <password>" to share a specific SSID and password
exit 1
else
echo "Sharing network \`$ssid\`"
fi
if [ -z "$ssid" ] || [ -z "$pw" ]; then
echo "Missing SSID or password"
exit 1
fi
if printf '%s' "$pw" | grep -qF ';'; then
echo "Password contains ;, I didn't look up how to handle that case"
exit 1
fi
rm -f /tmp/qr.png
printf '%s' "WIFI:T:WPA;S:$ssid;P:$pw;;" | qrencode -s 10 -o /tmp/qr.png
nsxiv /tmp/qr.png