summaryrefslogtreecommitdiff
path: root/Script/wifi
blob: 7b682b39c69ea379ab6c452d3872d17d94e50873 (plain)
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
#!/bin/bash

# Helper script to activate/desactivate WLAN from command line

# Usage :
# wifi connect <ssid> <passphrase>
# wifi disconnect <ssid> 
# wifi scan
# wifi status

#####################################################

### global vars ###

CONNMAN=/usr/sbin/connmanctl

script=$(basename $0)
configpath=/var/lib/connman # connman config directory
ssid="" 	# ssid of wifi network to reach 
passphrase="" 	# passphrase of wifi network to reach 
service=""	# service identifier of the network service to reach 
fullservice=""	# <ssid><service>

function error() {
	echo ERROR: "$@" >&2
	cat << EOF >&2
Usage: 
	$script connect <ssid> [passphrase]
	$script disconnect [ssid]
	$script scan
	$script status
EOF
	exit 1
}

function wifi_enable() {
	command=$($CONNMAN technologies | grep -A 4 wifi | sed -n '4p')

	if [[ $command == *True ]]; then
		echo ">>> Wifi is already enabled ... Ok"
	else
		echo ">>> Enabling wifi ..."
		command=$($CONNMAN enable wifi)
		if [[ $command == Enabled* ]]; then  
			echo $command
		else 
			echo ">>> Cannot enable wifi !"
			exit 1
		fi
	fi
}

function wifi_disable() {
	$CONNMAN disable wifi
}

function wifi_scan() {
	echo -e "\n>>> Scanning for available wifi networks..."
	$CONNMAN scan wifi
}

function wifi_connected() {
	command=$($CONNMAN technologies | grep -A 4 wifi | sed -n '5p')

	if [[ $command == *True ]];then
		echo -e "\n>>> Already connected to a wifi network"
		return 0
	fi
	return 1
}

function wifi_status() {
	echo -e "\n>>> Wifi status:"
	$CONNMAN technologies | grep -A 4 wifi
	echo -e "\n>>> Available SSIDs:"
	$CONNMAN services | grep wifi_
}

function wifi_config() {
	echo -e "\n>>> Setting up wifi connection..."

	retries=5

	while [ 1 ]; do
		$CONNMAN scan wifi
		echo "\n>>> Available SSIDs:"
		$CONNMAN services | grep wifi_

		fullservice=$($CONNMAN services | cut -c 5- | sed 's/ \+ /:/g' | grep "$ssid:")
		if [[ -n $fullservice ]]; then
			break
		fi

		retries=$(( retries - 1 ))
		if [[ $retries -gt 0 ]]; then
			echo "waiting for $ssid to appear..."
			sleep 5
			continue
		fi
		echo "Target ssid not found !"
		exit 1
	done

	ssid=$(echo "$fullservice" | awk -F: '{print $1}')
	service=$(echo "$fullservice" | awk -F: '{print $2}')

	echo "Target ssid found - config is :"
	echo "SSID       : $ssid"
	echo "Service Id : $service"
	echo "Passphrase : $passphrase"

	cat <<EOF >"$configpath/$ssid.config"
[service_$service]
Name = $ssid
Type = wifi
Passphrase = $passphrase
EOF

	echo "Configuration written"
}

function wifi_connect() {
	echo -e "\n>>> Performing connection ..."
	output=$($CONNMAN connect $service)

	if [[ $output == Connected* ]];then
		echo "...Ok."
		echo $output
	else
		echo "...connection failed !"
		echo $output

		wifi_disconnect 
		echo "Check your SSID or your passphrase"
		exit 1
	fi
}

function wifi_disconnect() {

	if [[ -z "$ssid" ]]; then
		$CONNMAN services | cut -c5- | sed 's/ \+ /:/g' | ( while read line; do
			ssid=$(cut -f1 -d':' <<<$line)
			serv=$(cut -f2 -d':' <<<$line)
			if [[ "$serv" =~ ^wifi_  && -f "$configpath/$ssid.config" ]]; then
				echo "Disconnecting $serv"
				$CONNMAN disconnect $serv
				echo "Cleaning config $ssid.config"
				rm -rf "$configpath/$ssid.config"
			fi
		done )
		return 0
	fi

	service=$($CONNMAN services | cut -c 5- | sed 's/ \+ /:/g' | grep "^$ssid:" | awk -F: '{print $2}')		
	if [[ -z "$service" ]]; then
		echo "... unknown service"
		rm -rf "$configpath/$ssid.config" # clear config in all cases
		return 1
	fi

	output=$($CONNMAN disconnect $service)
	rm -rf "$configpath/$ssid.config" # clear config in all cases
	if [[ $output == Disconnected* ]];then
		echo "...Ok."
		echo $output
	else
		echo "...disconnection failed !"
		echo $output
		return 1
	fi
}

case $1 in
	connect)
		ssid=$2
		passphrase=$3	
		[ -z "$ssid" ] && error "No ssid defined !"
		wifi_enable
		wifi_connected && exit 0
		wifi_config
		wifi_connect
		;;
	disconnect)
		ssid=$2	
		wifi_disconnect
		wifi_disable
		;;
	scan)
		wifi_enable
		wifi_scan
		wifi_status
		;;
	status)
		wifi_status
		;;
	*)
		error "Command line doesn't have any option !"
esac