-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifiman
executable file
·334 lines (332 loc) · 13.8 KB
/
wifiman
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
#!/bin/bash
###
if [ -z "$(command -v wpa_supplicant)" ]; then echo -e "\033[0;31mWPA_Supplicant not found.\033[0m"; exit 8; fi
if [ -z "$(command -v iwconfig)" ]; then echo -e "\033[0;31mwireless-tools not found.\033[0m"; exit 8; fi
if [ -z "$(command -v sed)" ]; then echo -e "\033[0;31msed not found.\033[0m"; exit 8; fi
if [ -z "$(command -v dhcpcd; command -v dhclient)" ]; then echo -e "\033[0;31mdhcp client not found.\033[0m"; exit 8; fi
if ! [ $UID == 0 ]; then echo "Run this script only as root."; exit 1; fi
if [ ! -f /etc/systemd/system/[email protected] ]; then
cat <<EOF > /etc/systemd/system/[email protected]
[Unit]
Description=WifiMan0
[Service]
Type=forking
ExecStart="/etc/wifiman0/.ids/%i/bashconf.sh"
[Install]
WantedBy=multi-user.target
EOF
fi
if [ ! -d /etc/wifiman0 ]; then mkdir /etc/wifiman0; fi
if [ ! -d /etc/wifiman0/.ids ]; then mkdir /etc/wifiman0/.ids; fi
if [ "$(ls -ld /etc/wifiman0 | cut -d' ' -f 1)" != "drwxr-----" ]; then chmod 740 /etc/wifiman0; fi
if ! [ "$(command -v rfkill)" ]; then true; else
if [ -n "$(rfkill -r | grep -Eo "wlan blocked")" ]; then
echo "It seems like your WLAN interface(s) blocked by something. Use rfkill unblock INTERFACE"
cont=1
fi
[[ $cont == 1 ]] && echo "Continue anyway?" && read ques && case $ques in Y*|y*) unset cont ;; *) exit 1 ;; esac; fi
if [ ! -f /etc/wifiman0/.dhcp ]; then
if [ "$(command -v dhcpcd dhclient | wc -l)" == 2 ]; then
echo -e "\033[0;32mIt seems like you have two DHCP clients.\033[0m \033[0;33mdhcpcd\033[0m and \033[0;33mdhclient\033[0m. Which one to use? (If you do not see the difference, choose \033[0;35mdhclient\033[0m) If you need to change this, execute \033[1;36mwifiman -u CLIENT\033[0m.
1. dhcpcd
2. dhclient"
read -rp "> " dhcp_choice
case $dhcp_choice in
1) future_dhcp=dhcpcd ;; 2) future_dhcp=dhclient ;; dhcp*) future_dhcp=dhcpcd ;; dhcl*) future_dhcp=dhclient ;; *) echo "Error. Unknown DHCP client."; exit 6 ;; esac
echo -e "You have chosen \033[0;33m${future_dhcp}\033[0m"; echo $future_dhcp > /etc/wifiman0/.dhcp
else
if [ -n "$(command -v dhcpcd)" ]; then future_dhcp=dhcpcd; echo $future_dhcp > /etc/wifiman0/.dhcp
else
if [ -n "$(command -v dhclient)" ]; then future_dhcp=dhclient; echo $future_dhcp > /etc/wifiman0/.dhcp; else echo -e "\033[0;31mDHCP client not found.\033[0m"; exit 8; fi
fi; fi; fi
dhcp_client="$(cat /etc/wifiman0/.dhcp)"
if [ $1 ]; then INTMOD=1
case "$1" in
-h|--help)
echo "-l/--list to list of available ESSID's
-cp/--create-profile [ "ESSID", interface, "password" ] to creating new Wi-Fi profile using wpa_supplicant
-lp/--list-profiles to view existing profiles
-dp/--delete-profile to delete existing profile
-c/--connect to connect using existing profile
-u/--update to update current dhcp client
-d/--disconnect to disconnect using existing profile
-sp/--show-password to show password from network profile"
;;
-l|--list)
iwlist scan 2>/dev/null | grep ESSID | sed 's/.*://' | sed -e 's/"//g' && true
;;
-cp|--create-profile)
essid_name=$2
interface_name=$3
essid_password=$4
if [ -z "$2" ]; then echo "second argument lost"; exit 2; fi
if [ -z "$3" ]; then echo "third argument lost"; exit 2; fi
if [ -z "$4" ]; then essid_password="key_mgmt=NONE"; else essid_password="psk=\"$4\""; fi
if [ "$interface_name" != "$(ls /sys/class/net | grep -x $interface_name)" ]; then echo -e "\033[1;31mIt seems like \033[0;33m$interface_name\033[0m \033[1;31mis not existing interface.\033[0m"; exit 3; fi
if [ -d /etc/wifiman0/"${essid_name}" ]; then REWRITE=1; else
mkdir /etc/wifiman0/"${essid_name}"; fi
cat <<EOF > /etc/wifiman0/"${essid_name}"/wpa.conf
network={
ssid="${essid_name}"
${essid_password}
}
EOF
cat <<EOF > /etc/wifiman0/"${essid_name}"/bashconf.sh
#!/bin/bash
wpa_supplicant -c /etc/wifiman0/"${essid_name}"/wpa.conf -i ${interface_name} &
${dhcp_client} ${interface_name} &
EOF
chmod +x /etc/wifiman0/"${essid_name}"/bashconf.sh
if ! [ $REWRITE ]; then
if [ "$(ls /etc/wifiman0 | wc -l)" == 1 ]; then touch /etc/wifiman0/"${essid_name}"/id1+; ln -s /etc/wifiman0/"${essid_name}" /etc/wifiman0/.ids/id1; else
ln -s /etc/wifiman0/"${essid_name}" /etc/wifiman0/.ids/$(ls --sort version /etc/wifiman0/*/id*+ | tac | rev | cut -c2-999 | cut -d"/" -f 1 | rev | cut -c3-999 | sort -V | tac | paste -sd " " | echo "id$(($(cut -d" " -f 1)+1))")
touch /etc/wifiman0/"${essid_name}"/$(ls --sort version /etc/wifiman0/*/id*+ | tac | rev | cut -c2-999 | cut -d"/" -f 1 | rev | cut -c3-999 | sort -V | tac | paste -sd " " | echo "id$(($(cut -d" " -f 1)+1))+")
fi; fi
unset REWRITE
echo -e "\033[1;32mConfiguration file created.\033[0m"
exit 0
;;
-lp|--list-profiles)
ls /etc/wifiman0 | cat
;;
-dp|--delete-profile)
essid_toremove=${@:2}
if [ -z "$2" ]; then echo "second argument lost"; exit 2; fi
if [ "${essid_toremove}" != "$(ls /etc/wifiman0 | grep -x "${essid_toremove}")" ]; then echo -e "\033[1;31mIt seems like \033[0;33m$essid_toremove\033[0m \033[1;31mis not exists.\033[0m"; exit 4; fi
essid_id_toremove="$(ls /etc/wifiman0/"${essid_toremove}"\/id*+ | rev | cut -d"/" -f 1 | cut -c2-999 | rev)"
if [ "$(systemctl is-active wifiman0@"${essid_id_toremove}".service)" == active ]; then systemctl stop wifiman0@"${essid_id_toremove}".service; fi
rm -rf /etc/wifiman0/"${essid_toremove}"
rm /etc/wifiman0/.ids/${essid_id_toremove}
echo -e "\033[1;32mProfile of \033[0;33m${essid_toremove}\033[0m \033[1;32mwas removed.\033[0m"
;;
-c|--connect)
essid_con=${@:2}
if [ -z "${essid_con}" ]; then echo "second argument lost"; exit 2; fi
if [ "${essid_con}" != "$(ls /etc/wifiman0 | grep -x "${essid_con}")" ]; then printf "\033[1;31mIt seems like \033[0;33m$essid_con\033[0m \033[1;31mprofile doesn't exists.\033[0m"; exit 4; fi
if [ -n "$(systemctl is-active wifiman0@\* | grep -x active)" ]; then echo "Turning off other profile"; systemctl stop wifiman0@\*; fi
essid_id_con="$(ls /etc/wifiman0/"${essid_con}"\/id*+ | rev | cut -d"/" -f 1 | cut -c2-999 | rev)"
systemctl start wifiman0@${essid_id_con}.service
if [ $? != 0 ]; then
echo -e "\033[0;31mSomething went wrong. (systemctl status wifiman0@${essid_id_con}.service)\033[0m"; exit 7
else echo "Done"
fi
;;
-u|--update)
if [ -z "$2" ]; then echo "second argument lost"; exit 2; fi
if [ "$(command -v dhcpcd dhclient | wc -l)" == 1 ]; then echo -e "You have only one DHCP client installed - \033[0;33m$(cat /etc/wifiman0/.dhcp)\033[0m"; exit 8; fi
if [ -z "$(ls /etc/wifiman0)" ]; then echo -e "\033[0;31mNo profiles found.\033[0m"; exit 4; fi
dhcp_choice=$2
case $dhcp_choice in
dhcpcd)
sed "3s/dhclient/dhcpcd/g" -i /etc/wifiman0/*/bashconf.sh
echo dhcpcd > /etc/wifiman0/.dhcp
echo Done
;;
dhclient)
sed "3s/dhcpcd/dhclient/g" -i /etc/wifiman0/*/bashconf.sh
echo dhclient > /etc/wifiman0/.dhcp
echo Done
;;
*)
echo "Unknown DHCP client. Use dhcpcd or dhclient"
exit 6
;;
esac
;;
-d|--disconnect)
if [ -n "$(systemctl is-active wifiman0@\* | grep -x active)" ]; then
systemctl stop wifiman0@\* &>/dev/null
echo "Done"
else
if [ -n "$(pidof wpa_supplicant)" ]; then killall wpa_supplicant; else
echo -e "\033[1;36mWifiMan doesn't running.\033[0m"; exit 5; fi; fi
;;
-sp|--show-password)
essid_ps=${@:2}
if [ -z "$2" ]; then echo "second argument lost"; exit 2; fi
if [ "${essid_ps}" != "$(ls /etc/wifiman0 | grep -x "${essid_ps}")" ]; then printf "\033[1;31mIt seems like \033[0;33m$essid_ps\033[0m \033[1;31mprofile doesn't exists.\033[0m"; exit 4; fi
if [ -n "$(cat /etc/wifiman0/"$essid_ps"/wpa.conf | sed '3!d' | sed "s/.*key_mgmt=//" | grep -x NONE)" ]; then echo "This network doesn't use a password."; else
cat /etc/wifiman0/"${essid_ps}"/wpa.conf | sed '1d; 2d; $d' | cut -d "\"" -f2- | sed 's/.$//'
fi
;;
*)
echo "Unknown argument"
exit 6
;;
esac
fi
startdialog() {
echo "Choose what to do"
echo "0. List available ESSID's
1. Create Wi-Fi profile using wpa_supplicant
2. List profiles
3. Delete existing profile
4. Connect using existing profile
5. Disconnect using existing profile
6. Get password from profile
7. Change DHCP client
8. Exit"
}
readthis() {
read -rp "> " $*
}
justcases() {
case $uclike in
0)
iwlist scan 2>/dev/null | grep ESSID | sed 's/.*://' | sed -e 's/"//g' && true
read
startdialog
readthis uclike
justcases
;;
1)
echo "Which interface to configure?(write carefully)"
ls /sys/class/net | sed ''/wlp/s//$(echo -e "\033[0;32mwlp\033[0m")/'' | sed ''/wlan/s//$(echo -e "\033[0;32mwlan\033[0m")/''
readthis interface_name
if ! [ $interface_name ]; then echo "lost argument"; exit 2; fi
if [ "$interface_name" != "$(ls /sys/class/net | grep -x $interface_name)" ]; then echo -e "\033[1;31mIt seems like \033[0;33m$interface_name\033[0m \033[1;31mis not existing interface.\033[0m"
startdialog
readthis uclike
justcases; fi
echo "Which netwotk to configure(ESSID)"
readthis essid_name
if ! [ "$essid_name" ]; then echo "lost argument"; exit 2; fi
echo "What is the password for the network?"
readthis essid_password
if [ -z "$essid_password" ]; then essid_password="key_mgmt=NONE"; else essid_password="psk=\"$essid_password\""; fi
if [ -d /etc/wifiman0/"${essid_name}" ]; then REWRITE=1; else
mkdir /etc/wifiman0/"${essid_name}"; fi
cat <<EOF > /etc/wifiman0/"${essid_name}"/wpa.conf
network={
ssid="${essid_name}"
${essid_password}
}
EOF
cat <<EOF > /etc/wifiman0/"${essid_name}"/bashconf.sh
#!/bin/bash
wpa_supplicant -c /etc/wifiman0/"${essid_name}"/wpa.conf -i ${interface_name} &
${dhcp_client} ${interface_name} &
EOF
chmod +x /etc/wifiman0/"${essid_name}"/bashconf.sh
if ! [ $REWRITE ]; then
if [ "$(ls /etc/wifiman0 | wc -l)" == 1 ]; then touch /etc/wifiman0/"${essid_name}"/id1+; ln -s /etc/wifiman0/"${essid_name}" /etc/wifiman0/.ids/id1; else
ln -s /etc/wifiman0/"${essid_name}" /etc/wifiman0/.ids/$(ls --sort version /etc/wifiman0/*/id*+ | tac | rev | cut -c2-999 | cut -d"/" -f 1 | rev | cut -c3-999 | sort -V | tac | paste -sd " " | echo "id$(($(cut -d" " -f 1)+1))")
touch /etc/wifiman0/"${essid_name}"/$(ls --sort version /etc/wifiman0/*/id*+ | tac | rev | cut -c2-999 | cut -d"/" -f 1 | rev | cut -c3-999 | sort -V | tac | paste -sd " " | echo "id$(($(cut -d" " -f 1)+1))+")
fi; fi
unset REWRITE
echo -e "\033[1;32mConfiguration file created.\033[0m"
echo " "
startdialog
readthis uclike
justcases
;;
2)
ls /etc/wifiman0 | cat
echo " "
startdialog
readthis uclike
justcases
;;
3)
echo "Which to remove?"
readthis essid_toremove
if ! [ "$essid_toremove" ]; then echo "lost argument"; exit 2; fi
if [ "${essid_toremove}" != "$(ls /etc/wifiman0 | grep -x "${essid_toremove}")" ]; then echo -e "\033[1;31mIt seems like \033[0;33m$essid_toremove\033[0m \033[1;31mis not exists.\033[0m"
startdialog
readthis uclike
justcases; fi
essid_id_toremove="$(ls /etc/wifiman0/"${essid_toremove}"\/id*+ | rev | cut -d"/" -f 1 | cut -c2-999 | rev)"
if [ "$(systemctl is-active wifiman0@"${essid_id_toremove}".service)" == active ]; then systemctl stop wifiman0@"${essid_id_toremove}".service; fi
rm -rf /etc/wifiman0/"${essid_toremove}"
rm /etc/wifiman0/.ids/${essid_id_toremove}
echo -e "\033[1;32mProfile of \033[0;33m${essid_toremove}\033[0m \033[1;32mwas removed.\033[0m"
startdialog
readthis uclike
justcases
;;
4)
echo "Which network to connect?"
readthis essid_con
if ! [ "$essid_con" ]; then echo "lost argument"; exit 2; fi
if [ "${essid_con}" != "$(ls /etc/wifiman0 | grep -x "${essid_con}")" ]; then echo -e "\033[1;31mIt seems like \033[0;33m$essid_con\033[0m \033[1;31mprofile doesn't exists.\033[0m"
startdialog
readthis uclike
justcases; fi
essid_id_con="$(ls /etc/wifiman0/"${essid_con}"\/id*+ | rev | cut -d"/" -f 1 | cut -c2-999 | rev)"
if [ -n "$(systemctl is-active wifiman0@\* | grep -x active)" ]; then echo "Turning off other profile"; systemctl stop wifiman0@\*; fi
systemctl start wifiman0@${essid_id_con}.service ||
echo -e "\033[0;31mSomething went wrong. (systemctl status wifiman0@${essid_id_con}.service)\033[0m";
exit 0
;;
5)
if [ -n "$(systemctl is-active wifiman0@\* | grep -x active)" ]; then
systemctl stop wifiman0@\* &>/dev/null
else
if [ -n "$(pidof wpa_supplicant)" ]; then killall wpa_supplicant; else
echo -e "\033[1;36mWifiMan doesn't running.\033[0m"
startdialog
readthis uclike
justcases
fi
fi
;;
6)
if [ -z "$(ls /etc/wifiman0)" ]; then echo -e "\033[0;31mNo profiles found.\033[0m"; startdialog; readthis uclike; justcases; fi
echo "Which password to see?"
readthis essid_ps
if ! [ "$essid_ps" ]; then echo "lost argument"; exit 2; fi
if [ "${essid_ps}" != "$(ls /etc/wifiman0 | grep -x "${essid_ps}")" ]; then echo -e "\033[1;31mIt seems like \033[0;33m$essid_ps\033[0m \033[1;31mprofile doesn't exists.\033[0m"
startdialog; readthis uclike; justcases; fi
if [ "$(cat /etc/wifiman0/"${essid_ps}"/wpa.conf | sed '3!d' | sed "s/.*key_mgmt=//" | grep -x NONE)" ]; then echo "This network doesn't use a password."; else
cat /etc/wifiman0/"${essid_ps}"/wpa.conf | sed '1d; 2d; $d' | cut -d "\"" -f2- | sed 's/.$//'
read
startdialog
readthis uclike
justcases
fi
;;
7)
if [ "$(command -v dhcpcd dhclient | wc -l)" == 1 ]; then echo -e "You have only one DHCP client installed - \033[0;33m$(cat /etc/wifiman0/.dhcp)\033[0m\n"; startdialog; readthis uclike; justcases; fi
if [ -z "$(ls /etc/wifiman0)" ]; then echo -e "\033[0;31mNo profiles found.\033[0m"; startdialog; readthis uclike; justcases; else
echo -e "Which DHCP client to use? (\033[0;33mdhcpcd\033[0m or \033[0;33mdhclient\033[0m)"
readthis dhcp_choice
case $dhcp_choice in
dhcpcd)
sed "3s/dhclient/dhcpcd/g" -i /etc/wifiman0/*/bashconf.sh
echo dhcpcd > /etc/wifiman0/.dhcp
echo -e "\033[1;32mDone\033[0m"
echo " "; startdialog; readthis uclike; justcases
;;
dhclient)
sed "3s/dhcpcd/dhclient/g" -i /etc/wifiman0/*/bashconf.sh
echo dhclient > /etc/wifiman0/.dhcp
echo -e "\033[1;32mDone\033[0m"
echo " "; startdialog; readthis uclike; justcases
;;
*)
echo "Unknown DHCP client. Use dhcpcd or dhclient"
echo " "
startdialog
readthis uclike
justcases
;;
esac
fi
;;
8)
exit 0
;;
*)
echo "Uknown argument"
startdialog
readthis uclike
justcases
;;
esac
}
if [ ! $INTMOD ]; then
if [ "$(systemctl is-active wifiman0@\* | grep -x active)" ]; then echo -e "\033[1;33mCurrently, network is working through WifiMan\033[0m"; fi
startdialog
readthis uclike
justcases
fi