File: wifi-qr

package info (click to toggle)
wifi-qr 0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 148 kB
  • sloc: sh: 300; makefile: 2
file content (385 lines) | stat: -rwxr-xr-x 10,405 bytes parent folder | download
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/bin/bash
#
# Lazy boring
# Ubuntu-MM
# Idea from Xaomi
# Ko Ko Ye <kokoye2007@gmail.com>
# Ubuntu Myanmar LoCo Team

WIFIS=''
SSID=''
KEEY=''
PSK=''
QSSID=''
QKEES=''
QSSIDO=''

RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
BLUE=$(tput setaf 4)
RESET=$(tput sgr0)
BOLD=$(tput bold)
VERSION=0.2

## 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 
#qrencode -l h -s 14 -o 

#GUI Mainmenu
main_menu() {

  CHOICE=$(zenity --list \
    --title="WiFi and QR Code" \
    --text="Choose:" \
    --radiolist \
    --width=300 --height=320 \
    --column="Check" --column="Option" \
    TRUE "Scan and connect" \
    FALSE "Share current WiFi" \
    FALSE "Share saved WiFi" \
    FALSE "See license" \
    FALSE "See version")

  #Menu Select
  if [[ "$CHOICE" =~ "Share saved WiFi" ]]; then
    call_wifi_gui

  elif [[ "$CHOICE" =~ "Share current WiFi" ]]; then
    call_current_wifi_gui
  
  elif [[ "$CHOICE" =~ "Scan and connect" ]]; then
    call_wifi_scan_gui

  elif [[ "$CHOICE" =~ "See license" ]]; then
    zenity --text-info --title="WiFi QR Copyright" --filename=/usr/share/doc/wifi-qr/copyright --width=528 --height=780
    main_menu

  elif [[ "$CHOICE" =~ "See version" ]]; then
    zenity --info --icon-name=wifi-qr --title="WiFi QR Version" --no-wrap --text=" This is wifi-qr version $VERSION"
    main_menu
  else
	exit 0
  fi
}


#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:)

  WIFIS=$(zenity --list --column="SSID" \
	  --width=300 --height=600 \
	  --title="Select SSID" \
	  "${wifilist[@]}")

  if [ -z "$WIFIS" ]; then
    exit 0
  else
    call_qr_gui
  fi
}

call_qr_gui() {
  call_wifi_pass
  QR_FILE_NAME="wifi-qr-$(echo "$WIFIS" | tr -dc '[:alnum:]').png"
  SAVE_QR_DIR="${HOME}/Pictures/"
  QR_PNG="$(zenity --file-selection --save --confirm-overwrite --filename="$SAVE_QR_DIR"/"$QR_FILE_NAME")"
  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;"
    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:)
  OLD_IFS=$IFS
  IFS=$'\n'
  for l in "${wifilist[@]}"; do
    array[$i]=$l
    i=$((i + 1))
  done

  i=0
  for m in "${array[@]}"; do
    echo
    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 -e "${BLUE}===========================================${RESET}"
  echo -e "\nYou choose SSID 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;"
  echo 
}

current_wifi_ssid() {
  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}"
}


#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")
  HIDN=$(nmcli -g 802-11-wireless.hidden connection show "$WIFIS")

  if [[ "$PSK" == *"wpa"* ]]; then
    PSSK="T:WPA;"
  elif
    [[ "$PSK" == "wep" ]]
  then
    PSSK="T:WEP;"
  else
   PSSK=""
  fi
  if [[ "$HIDN" == *"yes"* ]]; then
      H="H:true;"
  fi
  echo -e "${GREEN}"
  echo ""
  echo "SSID:    $SSID"
  echo "PASS:    $KEEY"
  echo "TYPE:    $PSSK"
  echo "HIDE:    $H"
  echo ""
  echo -e "${RESET}"
}

call_wifi_scan() {
  wifiqrdata=''
  # tmp data-matrix holder
  cwd="/tmp/"
  qr_data="$cwd/wifi-qr-scan"
  zbarcam --raw --prescale=320x240 /dev/video0 >$qr_data &

  # Last job running in background eg. zbarcam
  pid=$!

  # Sleep loop until $qr_data file has content
  while [[ ! -s $qr_data ]]; do
    sleep 1
    pgrep -x zbarcam >/dev/null && echo "Still Scanning" || exit 0
    # cleanup - add a trap that will remove $qr_data and kill zbarcam
    # if any of the signals - SIGHUP SIGINT SIGTERM it received.
    trap 'rm -f "$qr_data" ; kill -s 9 "$pid"; exit' SIGHUP SIGINT SIGTERM
  done

  kill -s 9 $pid
  wifiqrdata=$(cat $qr_data)
  rm -f $qr_data

  # Check if the QR contains WiFi data or not
  if echo "$wifiqrdata" | grep --quiet ^WIFI; then
    ## Go Go GUI and CLI Mod
    # ssid & key are not always in the same sequence - fix by using a dict and cut identifier for first key
    if echo "$wifiqrdata" | grep --quiet "H:true;"; then
    	QHIDE=true 
    fi
    declare -A wifiqrcred
    wifiqrcred+=([$(echo "$wifiqrdata" | cut -b 6- | awk -F';' '{print $1}' | cut -d":" -f1)]=$(echo "$wifiqrdata" | cut -b 6- | awk -F';' '{print $1}' | cut -d":" -f2))
    wifiqrcred+=([$(echo "$wifiqrdata" | awk -F';' '{print $2}' | cut -d":" -f1)]=$(echo "$wifiqrdata" | awk -F';' '{print $2}' | cut -d":" -f2))
    wifiqrcred+=([$(echo "$wifiqrdata" | awk -F';' '{print $3}' | cut -d":" -f1)]=$(echo "$wifiqrdata" | awk -F';' '{print $3}' | cut -d":" -f2))

    QSSID=${wifiqrcred[S]}
    QKEES=${wifiqrcred[P]}
    QTYPE=${wifiqrcred[T]}




    if [[ "$QHIDE" == "true" ]]; then
      echo -e "${GREEN}==================${RESET}"
      echo -e "${BOLD}${WHITE}It's is HIDDEN ${RESET}"
      echo -e "${GREEN}==================${RESET}"

    elif nmcli device wifi list | grep -w "$QSSID"; then
      echo -e "${GREEN}==================${RESET}"
      echo -e "${BOLD}${WHITE}$QSSID is Online ${RESET}"
      echo -e "${GREEN}==================${RESET}"
      QSSIDO=ON

    else
      echo -e "${RED}==================${RESET}"
      echo -e "${BOLD}${WHITE}Can't find $QSSID ${RESET}"
      echo -e "${RED}==================${RESET}"
      QSSIDO=OFF
    fi

  else
    echo -e "${RED}==================${RESET}"
    echo -e "${BOLD}${WHITE}It's not WiFi QR ${RESET}"
    echo -e "${RED}==================${RESET}"
    echo -e "${BLUE}==================${RESET}"
    echo -e "${BOLD}${BLUE} $wifiqrdata ${RESET}"
    echo -e "${BLUE}==================${RESET}"
    QSSIDO=NOWIFI
  fi

# key-mgmt type convert

  if [[ -z "$QTYPE" ]] ; then
    NTYPE="none"
  elif [[ "$QTYPE" == "WEP" ]] ; then
    NTYPE="ieee8021x"
  elif [[ "$QTYPE" == "WPA" ]] ; then
    NTYPE="wpa-psk"
  else
    echo "unknow or need to add key-mgmt type"
  fi


  # Append scan results to file
  #echo $wifiqrdata >> $ScanResult
}


call_wifi_scan_terminal() {
  call_wifi_scan
     if [[ "$QSSIDO" == "NOWIFI" ]]; then
	exit 0
     elif [[ "$QHIDE" == "true" ]]; then
	scan_connect_hidden
     elif [[ "$QSSIDO" == "ON" ]]; then
	scan_connect
     else
	echo "Can't find Network and Can't Connect"
	exit 0
     fi
}

call_wifi_scan_gui() {
  call_wifi_scan
  # Function with QR data
  if [[ "$QSSIDO" =~ "ON" ]]; then
    zenity --question --title="Connect to WiFi" --text="Connect to '$QSSID'?" --width=200 --height=120 --icon-name=network-wireless 2>/dev/null 
    connectn=$?
    if [ $connectn == 0 ]; then \
      scan_connect
    else
      exit
    fi
  elif [[ "$QSSIDO" =~ "OFF" ]]; then
    CHOICE=$(zenity --list \
      --title="Can't find WiFi" \
      --text="Choose:" \
      --radiolist \
      --width=400 --height=180 \
      --column="Check" --column="Option" \
      --icon-name=network-wireless-offline \
      TRUE "Retry scanning internally" \
      FALSE "Retry submitting another QR Code" )
    if [[ "$CHOICE" =~ "Retry scanning internally" ]]; then
      echo e "${BOLD}Scanning in progess ... ${RESET}"
      nmcli radio wifi off
      sleep 5
      scan_connect
      if [ $connectn != 0 ]; then
        echo -e "${RED}==================${RESET}"
        echo -e "${BOLD}${WHITE}Can't find $QSSID ${RESET}"
        echo -e "${RED}==================${RESET}"
        exit
      fi
    elif [[ "$CHOICE" =~ "Retry submitting another QR Code" ]]; then
      call_wifi_scan_gui
    else
      exit
    fi
  elif [[ "$QHIDE" =~ "true" ]]; then
    zenity --question --title="Connect to Hidden WiFi" --text="Connect to '$QSSID'?" --width=200 --height=120 --icon-name=network-wireless 2>/dev/null
    if [ $connectn = 0 ]; then
      scan_connect_hidden
    else
      exit
    fi
  fi
}

scan_connect() {
  nmcli radio wifi on
  nmcli dev wifi rescan >/dev/null 2>&1
  nmcli dev wifi connect "$QSSID" password "$QKEES" >/dev/null 2>&1
}

scan_connect_hidden() {
  nmcli radio wifi on
  nmcli con delete "$QSSID" >/dev/null 2>&1
  nmcli con add type wifi con-name "$QSSID" ssid "$QSSID" >/dev/null 2>&1
  nmcli con modify "$QSSID" wifi-sec.key-mgmt "$NTYPE" >/dev/null 2>&1
  nmcli con modify "$QSSID" wifi-sec.psk "$QKEES" >/dev/null 2>&1
  nmcli con modify "$QSSID" wifi.hidden yes  >/dev/null 2>&1
  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
  ;;
[Qq])
  echo "qr scan with gui"
  call_wifi_scan_gui
  ;;
[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
			q for QR Scan and Connect WiFi GUI
			v for WiFi-QR Version is $VERSION
		"
	nmcli device wifi show-password
  ;;
esac