-
Notifications
You must be signed in to change notification settings - Fork 0
/
qrcodes.sh
55 lines (46 loc) · 1.45 KB
/
qrcodes.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env bash
if iscommand qrencode; then
# encode stdin and clipboard to qrcode on terminal
alias qr='qrencode -t UTF8'
qrclip() { clipboard | qrencode -t UTF8; }
# print a qrcode with wifi credentials
wifiqr() {
usage() {
echo "usage: $ wifiqr <ssid> [<psk>]"
echo " ssid ssid or nmconnection name is required"
echo " psk if psk is given, qr code is generated directly"
}
qrcode() { # 1:ssid, 2:psk
# do not create a qr code directly, since it's trivial with a pipe
# and allows you to customize qrencode arguments easier
echo "WIFI:S:$1;T:WPA;P:$2;;"
}
# connection name or ssid is required
if [[ -z ${1+defined} ]] || [[ $1 = -h ]]; then
usage >&2; return 1
else
ssid="$1"
fi
# if psk is given, generate directly
if [[ -n ${2+defined} ]]; then
qrcode "$ssid" "$2"
return $?
fi
# check if nmconnection is a wifi and get psk
file="/etc/NetworkManager/system-connections/$ssid.nmconnection"
if ! [[ -r $file ]]; then
echo "err: file not readable: $file" >&2; return 1
elif ! grep '^type=wifi$' "$file" >/dev/null; then
echo "err: not a wifi connection: $file" >&2; return 1
elif ! grep '^psk=' "$file" >/dev/null; then
echo "err: psk field missing" >&2; return 1
fi
psk=$(sed -n "1,/^psk=/s/^psk=//p" "$file")
qrcode "$ssid" "$psk"
return $?
}
fi
if iscommand zbarcam; then
# read qr and barcodes with webcam silently
qrread() { zbarcam --raw --nodisplay; }
fi