Skip to content

Instantly share code, notes, and snippets.

@bitmvr
Last active July 18, 2021 19:38
Show Gist options
  • Save bitmvr/d6d0135e7e9a08ae5cfda452a7a894c6 to your computer and use it in GitHub Desktop.
Save bitmvr/d6d0135e7e9a08ae5cfda452a7a894c6 to your computer and use it in GitHub Desktop.
Generate Wi-Fi Config QR Code

Generate Wi-Fi Card on macOS

Create ./generate-wifi-config.sh

  1. Copy the contents of the code block below.
  2. Paste it into a file
  3. Save it as generate-wifi-config.sh
  4. Execute: chmod +x ./generate-wifi-config.sh to make it executable.

Note: Upon execution, your operating system is going to ask for your machine password. It does this because the script obtains your Wi-Fi password that is stored in the macOS Keychain.

#!/usr/bin/env bash


__getSSID(){
	/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk '/[[:space:]]+SSID/{print $2}'
}

__getAuthType(){
	my_auth_type="$(/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk '/link auth/{print $3}')" 

	case $my_auth_type in
		WEP*|wep*)
			echo "WEP"
		;;
		WPA*|wpa*)
			echo "WPA"
		;;
		WPA2-EAP)
			echo "WPA2-EAP"
		;;
	esac
}

__getWiFiPass(){
	mySSID="$1"
	security find-generic-password -a "${mySSID}" -w
}

main(){
	ssid="$(__getSSID)"
	auth_type="$(__getAuthType)"
	wifi_password="$(__getWiFiPass "${ssid}")"

	echo "WIFI:T:${auth_type};S:${ssid};P:${wifi_password};;"
}

main

Create QRCode

In order to create the qrcode, we need to install qrencode. The following step assume you have homebrew installed, if you do not, please install it.

brew install qrencode

Once you've installed qrencode, execute:

qrencode -o join-wifi-qrcode.png < <(./generate-wifi-card.sh)

Now grab your phone, unjoin your network, open join-wifi-qrcode.png and test your qr code!

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment