-
Notifications
You must be signed in to change notification settings - Fork 6
/
wificonnect
executable file
·176 lines (144 loc) · 3 KB
/
wificonnect
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
#!/bin/bash
#script to list available SSID and mod wpa-supplicant
#file to connect to new wifi hotspot
#this script works in conjuction with autohotspot
#http://www.raspberryconnect.com/network/item/330-raspberry-pi-auto-wifi-hotspot-switch-internet
#km4ack 20190401
#edited 20200221
mkdir -p $HOME/temp
TEMPFILE=$HOME/temp/tempwifi
HS=$(systemctl is-active autohotspot)
if [ $HS = 'active' ]
then
HSS=ACTIVE
else
HSS=INACTIVE
fi
ADD () {
# Generate List of Available SSIDs
SSID_LIST () {
LIST=$(iw dev "wlan0" scan ap-force | egrep "^BSS|SSID:" | grep SSID: | sed 's/SSID://' | awk '{ print $1 }' )
clear;echo;echo
echo "These SSID's are available"
echo
echo "$LIST" | sed '/^$/d'
#ask user about hotspots
echo
echo "1 to refresh the list, 2 to exit or"
echo "Enter name of SSID would you like add?"
read SSID
if [ $SSID == "1" ]
then
clear;echo;echo
echo "Scanning for WiFi again...please wait"
SSID_LIST
elif [ $SSID == "2" ]
then
echo "Goodbye"
exit
fi
}
#print list on screen
clear;echo;echo
echo "Scanning for Wifi.....please wait"
SSID_LIST
#get SSID password
echo
echo "What is the password for the hotspot?"
read PASS
#verify you want to add the hotspot
echo
echo "SSID = "$SSID
echo "Password = "$PASS
echo "REMEMBER THIS IS CASE SENSITIVE"
echo "Is this correct? y/n"
read ANSWER
#make the change to wpa_supplicant.conf
if [ $ANSWER == "y" ] || [ $ANSWER == "Y" ]
then
cat > $TEMPFILE <<EOF
network={
ssid="$SSID"
psk="$PASS"
key_mgmt=WPA-PSK
}
EOF
cat $TEMPFILE | tee -a /etc/wpa_supplicant/wpa_supplicant.conf
rm $TEMPFILE
if [ $HSS = 'ACTIVE' ]
then
echo "attempting to connect to new wifi"
/usr/bin/autohotspotN
elif [ $HSS = 'INACTIVE' ]
then
killall wpa_supplicant
sleep 1
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
sleep 2
fi
else
clear;echo;echo;
echo "Scanning for WiFi.....please wait"
SSID_LIST
fi
}
SUBTRACT () {
SSID=$(cat /etc/wpa_supplicant/wpa_supplicant.conf | grep ssid= | sed 's/ssid="//' | sed 's/"//')
clear
echo;echo
echo "List of currently known SSID's"
echo
echo "$SSID"
echo;echo
read -p "Name of SSID to remove or exit to quit " RSSID
if [ "$RSSID" = 'exit' ]
then
exit 0
fi
EXIST=$(echo $SSID | grep "$RSSID")
if [ -z "$EXIST" ]
then
cat <<EOF
That wifi doesn't exist on the pi.
Please check spelling and try again.
Remember that your answer is case
sensitive
EOF
sleep 5
exit 1
fi
file="/etc/wpa_supplicant/wpa_supplicant.conf"
foo="$(cat "$file" | awk -v RSSID="$RSSID" '$0 ~ RSSID { flag=1 }; flag==0 { print $0 }; /network={/ { flag=0 }' )"
if echo -e "$foo" | tail -1 | grep -q 'network={'; then
foo=$(echo -e "$foo" | head -n -1)
fi
#cat $foo | tee /etc/wpa_supplicant/wpa_supplicant.conf
echo -e "$foo" > "$file"
echo
echo $RSSID" has been removed"
sleep 2
}
CHOICES() {
clear;echo;echo
echo "Manage SSID's on the Pi"
echo;echo
echo "1 to remove WiFi"
echo "2 to add WiFi"
echo "3 to exit"
read ANS
if [ $ANS = '1' ]
then
SUBTRACT
elif [ $ANS = '2' ]
then
ADD
elif [ $ANS = '3' ]
then
exit 0
else
echo "No such option. Try again"
sleep 1
CHOICES
fi
}
CHOICES