-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathhlp_setup_net.sh
executable file
·299 lines (251 loc) · 7.55 KB
/
hlp_setup_net.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
#!/bin/bash
RESET="\033[0m"
BLACK="\033[30m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
MAGENTA="\033[35m"
CYAN="\033[36m"
WHITE="\033[37m"
BOLDBLACK="\033[1m\033[30m"
BOLDRED="\033[1m\033[31m"
BOLDGREEN="\033[1m\033[32m"
BOLDYELLOW="\033[1m\033[33m"
BOLDBLUE="\033[1m\033[34m"
BOLDMAGENTA="\033[1m\033[35m"
BOLDCYAN="\033[1m\033[36m"
BOLDWHITE="\033[1m\033[37m"
DEF_LLP_IP_DEV="10.42.0.34"
DEF_HLP_IP_DEV="10.42.0.36"
DEF_INTERFACE_DEV="eth0"
DEF_LLP_IP_EMU="10.42.0.31"
DEF_HLP_IP_EMU="10.42.0.33"
DEF_INTERFACE_EMU="eth1"
DEF_MASK="/24"
# You may edit these values if you happen to use this script a lot
llp_ip=$DEF_LLP_IP_DEV
hlp_ip=$DEF_HLP_IP_DEV
hlp_ip_mask=$DEF_MASK
hlp_interface=$DEF_INTERFACE_DEV
# Timeout for retrying (seconds). You may edit as well
timeout=30
# Initial wait
wait=
# Do NOT modify this variable
adb_device=
declare -A devices
# --------------------------------------------------- Functions -------------------------------------------------------
function isPackageInstalled {
echo $(dpkg-query -W -f='${Status}' $1 2>/dev/null | grep -c "ok installed")
}
function isDeviceOnline {
local device_present=$(adb devices -l | grep -c $adb_device)
if [ "$device_present" -eq 1 ]; then
echo true
else
# Devices are less or more than one. That's an error
echo false
fi
}
function findDevices {
local flag=false
while read -r device type
do
if ! $flag; then
flag=true
continue
fi
if [ "$device" != "" ]; then
devices[$device]=$type
fi
done < <(adb devices)
}
function isHlpAddressSet {
local result=$(adb -s $adb_device shell "ifconfig $hlp_interface" | grep -c $hlp_ip)
[[ $result = 1 ]] && echo true || echo false
}
function setupHlpNetwork {
if [ $(isHlpAddressSet) = false ]; then
adb -s $adb_device shell echo "ip link set dev $hlp_interface down" \| su
adb -s $adb_device shell echo "ip addr flush dev $hlp_interface" \| su
adb -s $adb_device shell echo "ip link set dev $hlp_interface up" \| su
adb -s $adb_device shell echo "ip addr add $hlp_ip$hlp_ip_mask dev $hlp_interface" \| su
adb -s $adb_device shell echo "ip rule flush" \| su
adb -s $adb_device shell echo "ip rule add pref 32766 from all lookup main" \| su
adb -s $adb_device shell echo "ip rule add pref 32767 from all lookup default" \| su
else
echo -e "\t- IP is already set"
fi
}
function checkNetworkConnection {
local is_link_to_mlp_ok=false
local is_link_to_hlp_ok=false
local output
output=$(ping -c1 $hlp_ip)
if [ $? -eq 0 ]; then
is_link_to_hlp_ok=true
fi
output=$(adb -s $adb_device shell "ping -c 1 $llp_ip &> /dev/null && echo 0 || echo 1")
if [ $output -eq 0 ]; then
is_link_to_mlp_ok=true
fi
$($is_link_to_hlp_ok && $is_link_to_mlp_ok) && echo true || echo false
}
function isBootCompleted {
local isBooting=$(adb -s $adb_device shell getprop init.svc.bootanim)
if [ "$isBooting" = "stopped" ]; then
echo 1
else
echo 0
fi
}
# -------------------------------------------- Check dependencies --------------------------------------------------
# Ensure ADB is installed
if [ $(isPackageInstalled "adb") = 0 ] && [ $(isPackageInstalled "android-tools-adb") = 0 ]; then
printf $RED"\nADB is not installed. Please install it and run the script again\n\n"$RESET
adb version
read -p "Press Return to exit"
exit 1
fi
# -------------------------------------------- Parse arguments --------------------------------------------------
# TODO Add validation
# Read arguments and set variables from them
while getopts :ebh:l:i:m:d:w:t: option
do
case "$option" in
e)
llp_ip=$DEF_LLP_IP_EMU
hlp_ip=$DEF_HLP_IP_EMU
hlp_ip_mask=$DEF_MASK
hlp_interface=$DEF_INTERFACE_EMU
;;
b)
llp_ip=$DEF_LLP_IP_DEV
hlp_ip=$DEF_HLP_IP_DEV
hlp_ip_mask=$DEF_MASK
hlp_interface=$DEF_INTERFACE_DEV
;;
h)
hlp_ip=$OPTARG
;;
l)
llp_ip=$OPTARG
;;
i)
hlp_interface=$OPTARG
;;
m)
hlp_ip_mask=$OPTARG
;;
d)
adb_device=$OPTARG
;;
t)
timeout=$OPTARG
;;
w)
wait=$OPTARG
;;
*)
printf $BOLDRED"\n > Invalid option received\n"$RESET
read -p "Press Return to exit"
exit 1
;;
esac
done
printf $BOLDWHITE"\n This script will set the Android-Ubuntu network for you\n\n"$RESET
# -------------------------------------------- Check HLP is online --------------------------------------------------
# Ensure ADB server is running
adb start-server
# Check for ADB devices
echo -e "\n > Searching devices..."
findDevices
# Checking device presence
counter=0
while [ ${#devices[@]} -eq 0 ]
do
if [ $counter -gt $timeout ]; then
# Timeout
printf $BOLDRED"\n > Timeout searching for devices. Shutting down...\n\n"$RESET
read -p "Press Return to exit"
exit 1
fi
# Retring...
printf $BOLDYELLOW"\t- No device found. We will try again...\n"$RESET
sleep 5
((counter+=5))
findDevices
done
# Ensure we only access one single ADB device
if [ ${#devices[@]} -gt 1 ]; then
# More than 1 device found
printf $BOLDYELLOW"\n > MORE than one device found. Please choose one the following and run the script again using -d option\n"$RESET
echo -e "\t- ${!devices[@]}"
read -p "Press Return to exit"
exit 1
elif [ "${devices[@]}" = "no permissions" ]; then
printf $BOLDYELLOW"\n > We found one device. But it DOES NOT have sufficient PERMISSIONS\n\n"$RESET
read -p "Press Return to exit"
exit 1
else
printf $BOLDGREEN"\n > We found one device. We will use:\n"$RESET
adb_device="${!devices[@]}"
echo -e "\t- $adb_device"
sleep 1
fi
if [ ! -z $wait ]; then
waiting_msg=$([[ $wait -eq -1 ]] && echo "until device is fully boot" || echo "$wait seconds or until device is fully boot")
printf $YELLOW"\n > Waiting for the device to fully boot...\n > We will wait $waiting_msg\n"$RESET
counter=0
while [[ $(isBootCompleted) -eq 0 && ( $wait -eq -1 || $counter -le $wait ) ]]
do
sleep 1
((counter+=1))
done
echo -e "\n > Waiting completed"
fi
# Safety wait
sleep 5
# ----------------------------------------------- Setup HLP network -------------------------------------------------------
# TODO Manage exceptions
echo -e "\n > Setting HLP network..."
setupHlpNetwork
# ---------------------------------------------- Double Check HLP Network Connectivity ---------------------------------------------
# Check ADB commands went OK
echo -e "\n > Checking HLP IP settings..."
count=0;
while [ $(isHlpAddressSet) = false ]
do
if [ $count -ge $timeout ]; then
printf $BOLDRED"\n > Too many failed attempts. Shutting down...\n"
printf "\n > RESULT: Fail - Unable to set configuration through ADB"$RESET
read -p "Press Return to exit"
exit 1
fi
printf $BOLDYELLOW"\t- A problem ocurred. Retrying...\n"$RESET
setupHlpNetwork
sleep 5
((count+=5))
done
printf $BOLDGREEN"\n > HLP IP address set successfully\n"$RESET
# Check network communication
echo -e "\n > Checking HLP-LLP network link..."
count=0;
while [ $(checkNetworkConnection) = false ]
do
# Reconfigure net and test again.
if [ $count -ge $timeout ]; then
printf $RED"\n > Timeout. Connection Test: Failed\n"$RESET
printf $BOLDYELLOW"\n > RESULT: Partial fail - HLP has configuration set, but we were unable to ping HLP/LLP. Is your Ethernet adapter ready?\n\n"$RESET
read -p "Press Return to exit"
exit 1
fi
printf $BOLDYELLOW"\t- Reconfiguring network and testing again\n"$RESET
setupHlpNetwork
sleep 5
((count+=5))
done
printf $BOLDGREEN"\n > Connection Test: Success!\n"
printf "\n > RESULT: Success!\n\n"$RESET
read -p "Press Return to exit"