-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.sh
executable file
·97 lines (77 loc) · 1.98 KB
/
control.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
#!/bin/bash
ip_list=()
DEFAULT_ADB_CLIENT_PORT=5555
function get_ips() {
ip_addresses=$( echo ${ADB_CLK_IPS} | tr -d '"')
for ip_address in ${ip_addresses}
do
num_valid_parts=0
parts=$( echo ${ip_address} | tr "." " ")
# check 4 parts for validity
for part in ${parts}; do
if [[ "${part}" =~ ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]]
then
num_valid_parts=$(( num_valid_parts+=1 ))
fi
done
if [ ${num_valid_parts} -eq 4 ]
then
echo "Valid IP address ${ip_address} was detected."
ip_list+=( ${ip_address} )
else
echo "IP address ${ip_address} does not seem valid."
fi
done
}
function check_ips() {
get_ips
if [ ${#ip_list[@]} -eq 0 ]
then
echo "No valid IPs detected."
exit 1
fi
}
function start() {
get_ips
echo "Starting ADB server..."
adb -a -P 5037 server nodaemon &
sever_wait=5
connect_retry=10
echo "Waiting ${sever_wait} seconds for the server to start."
sleep ${sever_wait}
while true
do
echo "Looking clients for connection..."
for ip in ${ip_list[@]}
do
is_connected=$(adb devices | awk '$2 == "device" { print $1; }' | awk -F ":" '{print $1}' | grep "${ip}")
if [ ${is_connected} = ${ip} ]
then
echo "ADB server already connected to client ${ip}."
else
echo "Connecting to ${ip} with port ${DEFAULT_ADB_CLIENT_PORT}."
adb connect ${ip}:${DEFAULT_ADB_CLIENT_PORT}
fi
done
echo "Waiting ${connect_retry} seconds for the next reconnect."
sleep ${connect_retry}
done
}
function stop() {
echo "Stopping ADB server..."
adb kill-server
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
esac