dotfiles/bin/qrwifi

35 lines
1,001 B
Bash
Executable file

#!/bin/sh
ssid="${1:-}"
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; }')"
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