Project

General

Profile

WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] » select_network.sh

Scans for available connections and lets you choose one to connect to. - Fil Bergamo, 11/21/2016 04:49 PM

 
1
#
2
#	     THIS IS FREE SOFTWARE 
3
#
4
# Copyright 2016 Filippo "Fil" Bergamo
5
# 
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
9
# 
10
#     http://www.apache.org/licenses/LICENSE-2.0
11
# 
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
17

    
18

    
19
ifacename=wlan0
20
scanfile=/data/misc/wifi/scanres.txt
21
socketfile=/data/misc/wifi/sockets
22
pidfile=/data/misc/wifi/pidfile
23
scriptWriteConf=/data/misc/wifi/write_config.sh
24
scriptStartWPA=/data/misc/wifi/startwpa.sh
25
scriptConnect=/data/misc/wifi/connect.sh
26
scriptDiscon=/data/misc/wifi/disconnect.sh
27

    
28
bash "$scriptDiscon"
29
if [ $? -ne 0 ]; then
30
	exit
31
fi
32
sleep 1s
33

    
34
bash $scriptStartWPA
35
if [ $? -ne 0 ]; then
36
	exit
37
fi
38
sleep 1s
39

    
40

    
41
# scan for networks
42
echo -n "Scanning for networks... "
43
wpa_cli -p$socketfile -P$pidfile -i$ifacename scan
44
if [ $? -ne 0 ]; then
45
	exit
46
fi
47
sleep 2s
48

    
49
# store the scan results on file
50
if [ -e $scanfile ]; then
51
	rm $scanfile
52
fi
53
sleep 1s
54

    
55
echo -n "Retrieving network list... "
56
wpa_cli -p$socketfile -P$pidfile -i$ifacename scan_results > $scanfile
57
if [ $? -ne 0 ]; then
58
	exit
59
fi
60
sleep 1s
61

    
62
# read the results in a variable:
63
networks=$(grep -v --invert-match '^bssid' $scanfile)
64

    
65
# count the number of networks:
66
netnum=$(echo "$networks" | wc -l)
67

    
68
# check if there is at least one network in the list
69
if [ "$netnum" == "" ] || [ "$netnum" -lt 1 ]; then
70
	echo "No networks found!"
71
	exit
72
fi
73

    
74

    
75
# store each network in array
76
idx=0
77
declare -a arrnets
78
printf %s "$networks" | while IFS= read -r line
79
do
80
	arrnets[$idx]="$line"
81
	idx=$(($idx+1))
82
done
83

    
84
if [ $? -ne 0 ]; then
85
	exit
86
fi
87

    
88
echo ""
89
echo "Please input the number of the network you want to connect to:"
90
echo -e "\t # |  SSID"
91
echo -e "\t---|------------------"
92

    
93
idx=1
94
declare -a AR
95
while IFS= read -r line
96
do
97
	# split the line into the different properties:
98
	read -r -a SUBAR <<< "$line"
99
	AR[$idx]=${SUBAR[4]} #the SSID is the 4th field
100
	echo -e "\t"$idx") | "${SUBAR[4]}
101

    
102
	idx=$(($idx+1))
103
done <<< "$networks"
104

    
105
# read user's choice
106
read selnet
107

    
108
if ! [[ "$selnet" =~ ^[0-9]+$ ]]; then
109
	echo "Invalid input!"
110
	exit
111
fi
112

    
113
if [ "$selnet" -le 0 ] || [ "$selnet" -gt "$netnum" ]; then
114
	echo "Invalid number!"
115
	exit
116
fi
117

    
118
selectedSSID=${AR[$selnet]}
119

    
120
echo "Enter a password for "$selectedSSID
121
read password
122

    
123
bash "$scriptWriteConf" $selectedSSID $password 
124
if [ $? -ne 0 ]; then
125
	exit
126
fi
127

    
128
sleep 1s
129

    
130
echo "Do you want to connect to "$selectedSSID" now? [y/n] "
131
read answer
132

    
133
if [ $answer = "y" ] || [ $answer = "Y" ]; then
134
	source $scriptConnect
135
fi
136

    
(2-2/7)