Skip to content

Instantly share code, notes, and snippets.

@GregDMeyer
Last active April 14, 2022 21:35
Show Gist options
  • Save GregDMeyer/d9a25430da3b7df17fe485cd52a19b73 to your computer and use it in GitHub Desktop.
Save GregDMeyer/d9a25430da3b7df17fe485cd52a19b73 to your computer and use it in GitHub Desktop.
a script to generate and show a QR-code containing the SSID and passphrase for the currently-connected WiFi network
#!/usr/bin/env bash
# a script to generate and show a QR-code containing the SSID and passphrase
# for the currently-connected WiFi network, for systems using NetworkManager
# the network device
DEVICE=wlp0s20f3
# need root to get password
if [ "$EUID" -ne 0 ]
then echo "This script must be run as root"
exit
fi
# get the currently connected network SSID
SSID=$(iw ${DEVICE} info | grep -Po '(?<=ssid ).*')
# the networkmanager config file that stores security info
NMPATH=/etc/NetworkManager/system-connections/${SSID}.nmconnection
# check if this network is secured
SEC=$(grep "wifi-security" ${NMPATH})
# if this network has no wifi-security, there is no password!
if [ -z "$SEC" ]; then
echo "Connected network \"${SSID}\" has no password!"
exit
fi
SEC=$(grep "wpa-psk" ${NMPATH})
# currently set up for wpa-psk only (doing WPA2 would be trivial too)
if [ -z "$SEC" ]; then
echo "Connected network \"${SSID}\" is not WPA-PSK security"
exit
fi
SSID_PASS=$(sudo cat ${NMPATH} | grep -Po '(?<=psk=).*')
qrencode -t UTF8 "WIFI:S:${SSID};T:WPA;P:${SSID_PASS};;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment