dotfiles/bin/qrwifi

46 lines
1.4 KiB
Text
Raw Permalink Normal View History

2023-07-26 15:47:29 +02:00
#!/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
2023-07-26 15:47:29 +02:00
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
2023-07-26 18:12:57 +02:00
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')"
2023-07-26 15:47:29 +02:00
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
fi
echo "$(tput bold)SSID$(tput sgr0) $ssid"
echo "$(tput bold)Password$(tput sgr0) $pw"
2023-07-26 15:47:29 +02:00
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