Skip to content

Commit

Permalink
WIFI SSID and Password: Special Characters
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoye2007 committed Apr 25, 2023
1 parent f1ae387 commit 49926e5
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 108 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ or using graphics menu
- [x] QR Scan Auto Connect Hidden Network
- [x] icons
- [x] Password with special characters needs to be unquoted
- [x] wifi config filename instad wifi ssid
- [ ] Additional LDAP Login

### Improve
Expand Down
260 changes: 171 additions & 89 deletions wifi-qr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Idea from Xaomi
# Ko Ko Ye <[email protected]>
# Ubuntu Myanmar LoCo Team
# https://github.com/kokoye2007/wifi-qr

WIFIS=''
SSID=''
Expand All @@ -21,13 +22,6 @@ RESET=$(tput sgr0)
BOLD=$(tput bold)
VERSION=0.2-3

## NMCLI no need SUDO ## 0.1.1
#Check root or sudo
#if [ "$(id -u)" != "0" ]; then
# echo "This script must be run as root" 1>&2
# exit 1
#fi

#RAW DATA SAMPLE
#"WIFI:S:$SSID;P:$PASSWORD;T:$KEY_TYPE;H:$true;"
#qrencode -o - -t UTF8
Expand Down Expand Up @@ -77,8 +71,8 @@ main_menu() {

#GUI
call_wifi_gui() {
# IFS=$'\r\n' GLOBIGNORE='*' command eval 'wifilist=($(nmcli -g NAME,TYPE connection | grep 802-11-wireless | cut -f1 -d:))'
mapfile -t wifilist < <(nmcli -g NAME,TYPE connection | grep 802-11-wireless | cut -f1 -d:)
# mapfile -t wifilist < <(nmcli -g NAME,TYPE connection | grep 802-11-wireless | sed 's/\\\([\\;,:"\]\)/\1/g' )
mapfile -t wifilist < <(nmcli -g NAME,TYPE connection | grep 802-11-wireless | sed 's/:802-11-wireless//' | sed 's/\\\([\\;,:"\]\)/\1/g' )

WIFIS=$(zenity --list --column="SSID" \
--width=300 --height=600 \
Expand All @@ -100,20 +94,24 @@ call_qr_gui() {
if [[ -z "$QR_PNG" ]]; then
main_menu
else
#QR_PNG="/tmp/wifi-qr-$(echo $WIFIS | tr -dc '[:alnum:]').png"
qrencode -l h -s 14 -o "$QR_PNG" "WIFI:S:$WIFIS;P:$KEEY;$PSSK$H;"
# QR_PNG="/tmp/wifi-qr-$(echo $WIFIS | tr -dc '[:alnum:]').png"
# WIFIS=$(echo $WIFIS | sed 's/\\\([\\;,:"\]\)/\1/g' )
# qrencode -o - -t UTF8 "WIFI:S:$SSID;P:$KEEY;$PSSK;$H;"
# qrencode -l h -s 14 -o "$QR_PNG" "WIFI:S:$WIFIS;P:$KEEY;$PSSK$H;"
qrencode -l h -s 14 -o "$QR_PNG" "WIFI:S:$SSID;P:$KEEY;$PSSK;$H;"

xdg-open "$QR_PNG"
fi
}

call_current_wifi_gui() {
current_wifi_ssid
call_qr_gui
}

#WiFi Command from terminal
call_wifi_terminal() {
# IFS=$'\r\n' GLOBIGNORE='*' command eval 'wifilist=($(nmcli -g NAME,TYPE connection | grep 802-11-wireless | cut -f1 -d":"))'
mapfile -t wifilist < <(nmcli -g NAME,TYPE connection | grep 802-11-wireless | cut -f1 -d:)
mapfile -t wifilist < <(nmcli -g NAME,TYPE connection | grep 802-11-wireless | sed 's/:802-11-wireless//' )
OLD_IFS=$IFS
IFS=$'\n'
for l in "${wifilist[@]}"; do
Expand All @@ -127,28 +125,28 @@ call_wifi_terminal() {
echo "$((i++))) $(basename "$m")"
done
IFS=$OLD_IFS

echo ""
echo -e "${BLUE}===============================================================${RESET}"
echo -e "${BOLD}${WHITE}please choose WiFi ssid ${RESET}"
echo -e "${BLUE}===============================================================${RESET}"
read -e -r wifissid
WIFIS=${array[$wifissid]}
echo hello - $WIFIS
# WIFIS=$(echo "${array[$wifissid]}" | sed 's/\\\([\\;,:"\]\)/\1/g' )
WIFIS="${array[$wifissid]}"
echo -e "${BLUE}===============================================================${RESET}"
echo -e "\nYou choose SSID is: $WIFIS\n ${RESET}"
echo -e "\nYou choose SSID File is: $WIFIS\n ${RESET}"
echo -e "${BLUE}===============================================================${RESET}"
terminal_qr
}

terminal_qr() {
call_wifi_pass
qrencode -o - -t UTF8 "WIFI:S:$WIFIS;P:$KEEY;$PSSK$H;"
# qrencode -o - -t UTF8 "WIFI:S:$WIFIS;P:$KEEY;$PSSK$H;"
qrencode -o - -t UTF8 "WIFI:S:$SSID;P:$KEEY;$PSSK;$H;"
echo
}

current_wifi_ssid() {
WIFIS="$(nmcli -t -f active,ssid dev wifi | grep -E '^yes' | cut -d':' -f2 | tr -d '\r\n')"
WIFIS="$(nmcli -t -f active,ssid dev wifi | grep -E '^yes' | cut -d':' -f2- | tr -d '\r\n')"
echo -e "${BLUE}===============================================================${RESET}"
echo -e "\nYou current active SSID is:${BOLD}${RED} $WIFIS\n ${RESET}"
echo -e "${BLUE}===============================================================${RESET}"
Expand All @@ -157,10 +155,11 @@ current_wifi_ssid() {

#Data Extractor nmcli version
call_wifi_pass() {
SSID="$WIFIS"
KEEY=$(nmcli -g 802-11-wireless-security.psk -s connection show "$WIFIS")
PSK=$(nmcli -g 802-11-wireless-security.key-mgmt connection show "$WIFIS" | sed 's/[\\;,:"\]/\\&/g' )
HIDN=$(nmcli -g 802-11-wireless.hidden connection show "$WIFIS")
WIFIS=$(echo "${WIFIS}" | sed 's/\\\([\\;,:"\]\)/\1/g' )
SSID=$(nmcli -g 802-11-wireless.ssid connection show "${WIFIS}" | sed 's/[;,"]/\\&/g' )
KEEY=$(nmcli -g 802-11-wireless-security.psk -s connection show "${WIFIS}" | sed 's/[;,"]/\\&/g')
PSK=$(nmcli -g 802-11-wireless-security.key-mgmt connection show "${WIFIS}" )
HIDN=$(nmcli -g 802-11-wireless.hidden connection show "${WIFIS}")

if [[ "$PSK" == *"wpa"* ]]; then
PSSK="T:WPA;"
Expand All @@ -176,8 +175,10 @@ call_wifi_pass() {
fi
echo -e "${GREEN}"
echo ""
echo "SSID: $SSID"
echo "PASS: $KEEY"
# echo "SSID: $SSID"
echo "SSID: $SSID" | sed 's/\\\([\\;,:"\]\)/\1/g'
# echo "PASS: $KEEY"
echo "PASS: $KEEY" | sed 's/\\\([\\;,:"\]\)/\1/g'
echo "TYPE: $PSSK"
echo "HIDE: $H"
echo ""
Expand Down Expand Up @@ -267,7 +268,7 @@ call_wifi_scan() {
echo -e "${BOLD}${BLUE} SSID: ${RESET} ${WHITE} $QSSID ${RESET}"
echo -e "${BOLD}${BLUE} TYPE: ${RESET} ${WHITE} $QTYPE ${RESET}"
echo -e "${BOLD}${BLUE} PASS: ${RESET} ${WHITE} $QKEES ${RESET}"
echo -e "${BOLD}${BLUE} PASS: ${RESET} ${WHITE} $QHIDE ${RESET}"
echo -e "${BOLD}${BLUE} HIDE: ${RESET} ${WHITE} $QHIDE ${RESET}"
echo -e "${GREEN}============================${RESET}"


Expand Down Expand Up @@ -388,15 +389,6 @@ call_file_select_and_scan_gui () {
call_wifi_scan_gui "$img_path"
}

call_file_select_and_scan_gui () {
img_path=$(zenity --file-selection)
if [ -z "$img_path" ]; then
echo "No file selected"
exit
fi
call_wifi_scan_gui "$img_path"
}

scan_connect() {
nmcli radio wifi on
nmcli dev wifi rescan >/dev/null 2>&1
Expand All @@ -413,58 +405,148 @@ scan_connect_hidden() {
nmcli con up "$QSSID" >/dev/null 2>&1
}

case $1 in

[Tt])
echo "start using wifi to qr"
call_wifi_terminal
;;
[Cc])
echo "start using wifi to qr"
call_wifi_gui
;;
[Gg])
echo "go go gui"
main_menu
;;
[Ss])
echo "qr scan"
call_wifi_scan_terminal "cam"
;;
[Ff])
if [ -f "$2" ]; then
echo "qr scan from file $2"
call_wifi_scan_terminal "$2"
else
echo "File not found"
exit
usage() {
echo ""
echo " Usage: $0 [-g] [-c] [-t] [-s] [-f file] [-p] [-q] [-v] [-h]"
echo ""
echo " -g Launch GUI Main Menu"
echo " -c Launch WiFi QR Create GUI"
echo " -t Launch WiFi QR Create Terminal"
echo " -s Launch QR Scan and Auto Connect WiFi"
echo " -f file Terminal [file] QR Scan and Auto Connect WiFi from file"
echo " -p Launch GUI [file] QR Scan and Auto Connect WiFi from file"
echo " -q Launch QR Scan and Connect WiFi GUI"
echo " -v Show WiFi-QR Version $VERSION"
echo " -h Show this help message"
echo ""
}

check_file() {
if [ ! -f "$1" ]; then
echo "Error: $1 is not a file."
exit 1
fi
;;
[Pp])
echo "qr scan from file with gui"
call_file_select_and_scan_gui
;;
[Qq])
echo "qr scan with gui"
call_wifi_scan_gui "cam"
;;
[Vv])
echo -e "${GREEN}============================${RESET}"
echo -e "${BOLD}${WHITE} WiFI-QR v $VERSION ${RESET}"
echo -e "${GREEN}============================${RESET}"
;;
*)
echo -e "\nInvalid input\n
Please use g - for GUI Main Menu
c - for WiFi QR Create GUI
t - for WiFi QR Create Terminal
s - for QR Scan and Auto Connect WiFi
- QR Scan and Auto Connect WiFi from file
f - Terminal [file]
p - With GUI [file]
q - for QR Scan and Connect WiFi GUI
v - for WiFi-QR Version is $VERSION
"
nmcli device wifi show-password
;;
esac

file_type=$(file -b --mime-type "$1")
if [ "$file_type" != "image/png" ] && [ "$file_type" != "image/jpeg" ]; then
echo "Error: $1 is not an image file."
exit 1
fi
}

if [[ $# -eq 0 ]] || [[ $1 != -* ]]; then
usage
exit 0
fi

while getopts ":gctsf:pqvh" opt; do
case $opt in
g)
echo "Launching GUI Main Menu..."
main_menu
;;
c)
echo "Launching WiFi QR Create GUI..."
call_wifi_gui
;;
t)
echo "Launching WiFi QR Create Terminal..."
call_wifi_terminal
;;
s)
echo "Launching QR Scan and Auto Connect WiFi..."
call_wifi_scan_terminal "cam"
;;
f)
check_file "$OPTARG"
echo "Launching Terminal [file] QR Scan and Auto Connect WiFi from file: $OPTARG"
call_wifi_scan_terminal "$2"
;;
p)
echo "Launching GUI [file] QR Scan and Auto Connect WiFi from file"
call_file_select_and_scan_gui
;;
q)
echo "Launching QR Scan and Connect WiFi GUI..."
call_wifi_scan_gui "cam"
;;
v)
echo -e "${GREEN}============================${RESET}"
echo -e "${BOLD}${WHITE} WiFI-QR $VERSION ${RESET}"
echo -e "${GREEN}============================${RESET}"
;;
h)
usage
nmcli device wifi show-password
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
*)
usage
exit 0
esac
done


# case $1 in
#
# [Tt])
# echo "start using wifi to qr"
# call_wifi_terminal
# ;;
# [Cc])
# echo "start using wifi to qr"
# call_wifi_gui
# ;;
# [Gg])
# echo "go go gui"
# main_menu
# ;;
# [Ss])
# echo "qr scan"
# call_wifi_scan_terminal "cam"
# ;;
# [Ff])
# if [ -f "$2" ]; then
# echo "qr scan from file $2"
# call_wifi_scan_terminal "$2"
# else
# echo "File not found"
# exit
# fi
# ;;
# [Pp])
# echo "qr scan from file with gui"
# call_file_select_and_scan_gui
# ;;
# [Qq])
# echo "qr scan with gui"
# call_wifi_scan_gui "cam"
# ;;
# [Vv])
# echo -e "${GREEN}============================${RESET}"
# echo -e "${BOLD}${WHITE} WiFI-QR v $VERSION ${RESET}"
# echo -e "${GREEN}============================${RESET}"
# ;;
# *)
# echo -e "\nInvalid input\n
# Please use g - for GUI Main Menu
# c - for WiFi QR Create GUI
# t - for WiFi QR Create Terminal
# s - for QR Scan and Auto Connect WiFi
# - QR Scan and Auto Connect WiFi from file
# f - Terminal [file]
# p - With GUI [file]
# q - for QR Scan and Connect WiFi GUI
# v - for WiFi-QR Version is $VERSION
# "
# nmcli device wifi show-password
# ;;
# esac
Loading

0 comments on commit 49926e5

Please sign in to comment.