-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwifi_usual_JBs.sh
executable file
·331 lines (271 loc) · 12.9 KB
/
wifi_usual_JBs.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
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
#!/bin/bash
#
# Autor= João Batista Ribeiro
# Bugs, Agradecimentos, Críticas "construtivas"
# me envie um e-mail. Ficarei Grato!
# e-mail: [email protected]
#
# Este programa é um software livre; você pode redistribui-lo e/ou
# modifica-lo dentro dos termos da Licença Pública Geral GNU como
# publicada pela Fundação do Software Livre (FSF); na versão 2 da
# Licença, ou (na sua opinião) qualquer versão.
#
# Este programa é distribuído na esperança que possa ser útil,
# mas SEM NENHUMA GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a
# qualquer MERCADO ou APLICAÇÃO EM PARTICULAR.
#
# Veja a Licença Pública Geral GNU para mais detalhes.
# Você deve ter recebido uma cópia da Licença Pública Geral GNU
# junto com este programa, se não, escreva para a Fundação do Software
#
# Livre(FSF) Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Script: usual / common day-to-day functions with Wi-Fi
#
# Last update: 29/06/2023
#
# Tip: If have NetworkManager installed and running, you can use nmtui
#
useColor() {
BLACK='\e[1;30m'
RED='\e[1;31m'
GREEN='\e[1;32m'
NC='\033[0m' # reset/no color
BLUE='\e[1;34m'
PINK='\e[1;35m'
CYAN='\e[1;36m'
WHITE='\e[1;37m'
}
emptySpaces=$1 # Remove empty space form calls in this script to itself with $colorPrint empty
if [ "$emptySpaces" == '' ]; then
shift
fi
notUseColor() {
unset BLACK RED GREEN NC BLUE PINK CYAN WHITE
}
colorPrint=$1
if [ "$colorPrint" == "noColor" ]; then
echo -e "\nColors disabled"
shift
else # Some colors for script output - Make it easier to follow
useColor
colorPrint=''
fi
notPrintHeaderHeader=$1
if [ "$notPrintHeaderHeader" != "notPrintHeader" ]; then
echo -e "$BLUE #___ Script to usual commands ___#$NC\n"
else
shift
fi
testColorInput=$1
if [ "$testColorInput" == "testColor" ]; then
echo -e "\n Test colors: $RED RED $WHITE WHITE $PINK PINK $BLACK BLACK $BLUE BLUE $GREEN GREEN $CYAN CYAN $NC NC\n"
shift
fi
loadDevWirelessInterface() {
devInterface=$1
if [ "$devInterface" == '' ]; then
devInterface="wlan0"
fi
echo -e "\nWorking with the interface: $devInterface"
echo -e "You can pass other interface as a parameter\n"
}
insertRootPasswordMsg() {
if [ "$(whoami)" != "root" ]; then
echo -e "$CYAN\nInsert the root password to continue$NC\n"
fi
}
optionInput=$1
case $optionInput in
"ap-info" )
echo -e "$CYAN# Show information about the AP connected #$NC"
loadDevWirelessInterface "$2"
echo -e "/usr/sbin/iw dev $devInterface link:"
/usr/sbin/iw dev "$devInterface" link
echo -e "\n/sbin/iwconfig $devInterface:"
/sbin/iwconfig "$devInterface"
;;
"--help" | "-h" | "help" | 'h' | '' | 'w' )
if [ "$optionInput" == '' ] || [ "$optionInput" == 'w' ]; then # whiptailMenu()
notUseColor
fi
# Options text
optionVector=("ap-info " " - Show information about the AP connected"
"cn " "$RED * - Connect to Wi-Fi network (in /etc/wpa_supplicant.conf)"
"cr " "$RED * - Create configuration to connect to Wi-Fi network (in /etc/wpa_supplicant.conf)"
"dc " "$RED * - Disconnect to one Wi-Fi network"
"h " " - Show this help message (the same result with \"help\", \"--help\", \"-h\" or 'help')"
"l-iw " "$RED * - List the Wi-Fi AP around, with iw (show WPS and more infos)"
"l-iwlist " " - List the Wi-Fi AP around, with iwlist (show WPA/2 and more infos)"
"nm-list " "$PINK + - List the Wi-Fi AP around with the nmcli from NetworkManager"
"w " " - Menu with whiptail, where you can call the options above (the same result with 'w' or '')")
if [ "$colorPrint" == '' ]; then # set useColor on again if the user not pass "noColor"
useColor
fi
case $optionInput in
"--help" | "-h" | "help" | 'h' )
help() {
echo -e "$CYAN# Show this help message (the same result with \"help\", \"--help\", \"-h\" or 'h') $NC"
echo -e "$CYAN\nOptions:\n$RED Obs$CYAN:$RED * root required,$PINK + NetworkManager required$CYAN\n"
countOption=0
optionVectorSize=${#optionVector[*]}
while [ "$countOption" -lt "$optionVectorSize" ]; do
echo -e " $GREEN${optionVector[$countOption]}$CYAN ${optionVector[$countOption+1]}$NC"
countOption=$((countOption + 2))
done
}
help
;;
'' | 'w' )
whiptailMenu() {
echo -e "$CYAN# Menu with whiptail, where you can call the options above (the same result with 'w' or '') #$NC\n"
eval "$(resize)"
heightWhiptail=$((LINES - 5))
widthWhiptail=$((COLUMNS - 5))
if [ "$LINES" -lt 16 ]; then
echo -e "$RED\nTerminal with very small size. Use one terminal with at least 16 columns (actual size line: $LINES columns: $COLUMNS)$NC"
echo -e "$GREEN\nRunning: $0 $colorPrint notPrintHeader --help$CYAN\n" | sed 's/ / /g'
$0 "$colorPrint" notPrintHeader --help
else
heightMenuBoxWhiptail=$((LINES - 15))
#whiptail --title "<título da caixa de menu>" --menu "<texto a ser exibido>" <altura> <largura> <altura da caixa de menu> \
#[ <tag> <item> ] [ <tag> <item> ] [ <tag> <item> ]
itemSelected=$(whiptail --title "#___ Script to usual commands ___#" --menu "Obs: * root required, + NetworkManager required, = X server required
Options:" $heightWhiptail $widthWhiptail $heightMenuBoxWhiptail \
"${optionVector[0]}" "${optionVector[1]}" \
"${optionVector[2]}" "${optionVector[3]}" \
"${optionVector[4]}" "${optionVector[5]}" \
"${optionVector[6]}" "${optionVector[7]}" \
"${optionVector[8]}" "${optionVector[9]}" \
"${optionVector[10]}" "${optionVector[11]}" \
"${optionVector[12]}" "${optionVector[13]}" \
"${optionVector[14]}" "${optionVector[15]}" 3>&1 1>&2 2>&3)
if [ "$itemSelected" != '' ]; then
itemSelected=${itemSelected// /} # Remove space in the end of selected item
echo -e "$GREEN\nRunning: $0 $colorPrint notPrintHeader $itemSelected ${*} $CYAN\n" | sed 's/ / /g'
$0 "$colorPrint" notPrintHeader "$itemSelected" "${*}"
fi
fi
}
whiptailMenu "${*:2}"
;;
esac
;;
"cr" )
echo -e "$CYAN# Create configuration to connect to Wi-Fi network (in /etc/wpa_supplicant.conf) #$NC\n"
loadDevWirelessInterface "$2"
echo "Visible networks:"
/sbin/iwlist "$devInterface" scan | grep "ESSID" | uniq
createWifiConfig() {
continueOrNot=0
while [ "$continueOrNot" == 0 ]; do
echo -en "\nName of the network (SSID): "
read -r netSSID
echo -en "\nPassword of this network: "
read -r -s netPassword
if [ "$(wc -c <<< "$netPassword")" -lt 8 ]; then
echo -e "\n\nError: Passphrase must be 8..63 characters\n Insert the values again!"
else
continueOrNot=1
fi
done
wpa_passphrase "$netSSID" "$netPassword" | grep -v "#psk" >> /etc/wpa_supplicant.conf
echo -e "\nConfiguration of the network \"$netSSID\" saved\n"
}
export -f createWifiConfig
insertRootPasswordMsg
su root -c 'createWifiConfig' # In this case without the hyphen (su - root -c 'command') to no change the environment variables
;;
"cn" )
echo -e "$CYAN# Connect to Wi-Fi network (in /etc/wpa_supplicant.conf) #$NC\n"
loadDevWirelessInterface "$2"
networkConfigAvailable=$(grep "ssid" < /etc/wpa_supplicant.conf)
if [ "$networkConfigAvailable" == '' ]; then
echo -e "$RED\nError: Not fond any configuration of Wi-Fi networks (in /etc/wpa_supplicant.conf).\n Try: $0 cr$NC"
else
connectWifiConfig() {
if pgrep -f "NetworkManager" > /dev/null; then # Test if NetworkManager is running
echo -en "\nKilling NetworkManager..."
killall NetworkManager # kill the previous wpa_supplicant "configuration"
fi
if pgrep -f "wpa_supplicant" > /dev/null; then # Test if wpa_supplicant is running
echo -en "\nKilling wpa_supplicant..."
killall wpa_supplicant # kill the previous wpa_supplicant "configuration"
fi
echo -e "\nChoose one network to connect\n"
TmpFile=$(mktemp) # Create a tmp file
netNumber=1
networkName=$(grep "ssid" < /etc/wpa_supplicant.conf)
for value in $networkName; do
echo " $netNumber $value" >> "$TmpFile"
((netNumber++))
done
cat "$TmpFile"
echo -en "\nNetwork number: "
read -r networkNumber
networkName=$(grep "$networkNumber ssid" < "$TmpFile")
networkName=$(sed 's/^.* s/s/g' <<< "$networkName")
rm "$TmpFile" # Delete the tmp file
#sed -n '/Beginning of block/!b;:a;/End of block/!{$!{N;ba}};{/some_pattern/p}' fileName # sed in block text
wpaConf=$(sed -n '/network/!b;:a;/}/!{$!{N;ba}};{/'"$networkName"'/p}' /etc/wpa_supplicant.conf)
if [ "$wpaConf" == '' ]; then
echo -e "$RED\nError: Not find configuration to network '$networkName' (in /etc/wpa_supplicant.conf).\n Try: $scriptName cr$NC"
else
TmpFile=$(mktemp)
grep -v -E "{|}|ssid|psk" < /etc/wpa_supplicant.conf > "$TmpFile"
echo -e "$wpaConf" >> "$TmpFile" # Save the configuration of the network on this file
echo -e "\n########### Network configuration ####################"
cat "$TmpFile"
echo -e "######################################################\n"
echo -e "\nConnecting to $networkName by the device $devInterface\n"
#wpa_supplicant -i wlan0 -c /etc/wpa_supplicant.conf -d -B wext # Normal command
wpa_supplicant -i "$devInterface" -c "$TmpFile" -d -B wext # Connect with the network using the tmp file
rm "$TmpFile"
echo -e "\nGetting IP\n"
dhclient "$devInterface" # Get IP
echo -e "\nNetwork status\n"
iw dev "$devInterface" link # Show connection status
fi
}
export -f connectWifiConfig
export devInterface
scriptName=$0
export scriptName
insertRootPasswordMsg
su root -c 'connectWifiConfig' # In this case without the hyphen (su - root -c 'command') to no change the environment variables
fi
;;
"dc" )
echo -e "$CYAN# Disconnect of one Wi-Fi network #$NC\n"
loadDevWirelessInterface "$2"
insertRootPasswordMsg
su - root -c "dhclient -r $devInterface
ifconfig $devInterface down
iw dev $devInterface link"
;;
"l-iw" )
echo -e "$CYAN# List the Wi-Fi AP around, with iw (show WPS and more infos) #$NC\n"
loadDevWirelessInterface "$2"
insertRootPasswordMsg
su - root -c "/usr/sbin/iw dev $devInterface scan | grep -E '$devInterface|SSID|signal|WPA|WEP|WPS|Authentication|WPA2|: channel'"
;;
"l-iwlist" )
echo -e "$CYAN# List the Wi-Fi AP around, with iwlist (show WPA/2 and more infos) #$NC\n"
loadDevWirelessInterface "$2"
insertRootPasswordMsg
/sbin/iwlist "$devInterface" scan | grep -E "Address|ESSID|Frequency|Signal|WPA|WPA2|Encryption|Mode|PSK|Authentication"
;;
"nm-list" )
echo -e "$CYAN# List the Wi-Fi AP around with the nmcli from NetworkManager #$NC\n"
nmcli device wifi list
;;
* )
echo -e "\n $(basename "$0") - Error: Option \"$1\" not recognized"
echo -e " Try: $0 '--help'"
;;
esac
if [ "$notPrintHeaderHeader" != "notPrintHeader" ]; then
echo -e "$BLUE\n #___ So Long, and Thanks for All the Fish ___#$NC"
else
shift
fi