-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathphablet-network-setup
188 lines (161 loc) · 5.01 KB
/
phablet-network-setup
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
#!/bin/bash
# This program is free software: you can redistribute it and/or modify it
# under the terms of the the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the applicable version of the GNU General Public
# License for more details.
#.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2012 Canonical, Ltd.
export LC_ALL=C
usage() {
cat <<EOF
usage: $0 [OPTIONS]
Copies ACTIVE network manager connection into device
OPTIONS:
-h Show this message
-s Specify the serial of the device to install (see adb $ADBOPTS devices)
-i Install helper network packages (openssh-server, iw) in chroot
-n Select network file
EOF
}
ADBOPTS=""
OPTION_SSH=0
OPTION_NETWORK_FILE=""
CHROOTDIR="/data/ubuntu"
CHROOTCMD="chroot $CHROOTDIR"
while getopts "hin:s:" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
n)
OPTION_NETWORK_FILE="$OPTARG"
;;
i)
OPTION_SSH=1
;;
s)
ADBOPTS="-s $OPTARG"
;;
?)
usage
exit
;;
esac
done
shift $(($OPTIND - 1))
NETWORK_MANAGER=/etc/NetworkManager/system-connections
USER=phablet
is_flipped() {
# Checks if the devices uses a flipped image
#
# @return: 0 if image is flipped 1 otherwise
[ -n "$(adb $ADBOPTS shell "ls /etc/lsb-release 2>/dev/null")" ] && return 0
return 1
}
get_user_id() {
adb $ADBOPTS shell $CHROOTCMD /usr/bin/id -u $1 | tr -d '\r'
}
install_ssh_key() {
HOME_DIR=$CHROOTDIR/home/$USER
USER_ID=$(get_user_id $USER)
adb $ADBOPTS push ~/.ssh/id_rsa.pub $HOME_DIR/.ssh/authorized_keys
adb $ADBOPTS shell chown $USER_ID:$USER_ID $HOME_DIR/.ssh
adb $ADBOPTS shell chown $USER_ID:$USER_ID $HOME_DIR/.ssh/authorized_keys
adb $ADBOPTS shell chmod 700 $HOME_DIR/.ssh
adb $ADBOPTS shell chmod 600 $HOME_DIR/.ssh/authorized_keys
}
find_active_network() {
wireless_adapter="$(nmcli -t -f device,type dev | egrep wireless | cut -d: -f1)"
network_active_uuid="$(nmcli -t -f name,uuid,devices,vpn con status | grep $wireless_adapter | egrep ":no$"| cut -d: -f2)"
network_file=$(sudo grep "$network_active_uuid" $NETWORK_MANAGER/* | grep uuid | cut -d: -f1)
if [ -z "$network_active_uuid" ]
then
echo "No active wifi network connection, exiting"
exit 1
fi
echo "$network_file"
}
if is_flipped; then
echo Ubuntu is root
CHROOTDIR=""
CHROOTCMD=""
fi
if [ -z "$OPTION_NETWORK_FILE" ]; then
network_file=$(find_active_network)
else
network_file=$OPTION_NETWORK_FILE
fi
if [ ! -f "$network_file" ]
then
echo "Network connection file \"$network_file\" cannot be read"
exit 1
fi
echo Network file is $network_file
TMP_FILE=$(mktemp)
sudo grep -v mac-address "$network_file" > $TMP_FILE
echo Provisioning network file to device
adb $ADBOPTS root
adb $ADBOPTS wait-for-device
target_network_file=$CHROOTDIR/$NETWORK_MANAGER/active_ws_connection.conf
adb $ADBOPTS push $TMP_FILE $target_network_file
adb $ADBOPTS shell "chmod 600 $target_network_file"
rm -f $TMP_FILE
echo
echo Network setup complete
if [ $OPTION_SSH -eq 1 ]; then
TMP_FILE=$(mktemp)
# Indentation is going to be ugly here
cat > $TMP_FILE << 'EOF'
#!/bin/bash
export PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install --yes --quiet openssh-server iw
EOF
echo Installing ssh
nmcli_cmd="adb $ADBOPTS shell $CHROOTCMD $CHROOTDIR/usr/bin/nmcli"
wait=0
iface=$($nmcli_cmd -t -f devices con status | tr -d '\r')
until [ -n "$iface" ] || [ $wait -eq 10 ]; do
echo "Network not ready, sleeping"
sleep $(( wait++ ))
iface=$($nmcli_cmd -t -f devices con status | tr -d '\r')
done
if [ -z "$iface" ]; then
echo "Network setup timed out"
exit 1
fi
wait=0
ip_data=$($nmcli_cmd -t dev list iface $iface | grep ADDRESS)
until [ -n "$ip_data" ] || [ $wait -eq 10 ]; do
echo "Network not ready, sleeping"
sleep $(( wait++ ))
ip_data=$($nmcli_cmd -t dev list iface $iface | grep ADDRESS)
done
if [ -z "$ip_data" ]; then
echo "Network setup timed out"
exit 1
fi
adb $ADBOPTS push $TMP_FILE $CHROOTDIR/$TMP_FILE
adb $ADBOPTS shell chmod 755 $CHROOTDIR/$TMP_FILE
adb $ADBOPTS shell $CHROOTCMD $CHROOTDIR/$TMP_FILE
rm -f $TMP_FILE
echo "Installing ssh keys"
install_ssh_key
echo
echo openssh-server install complete, to connect execute
echo adb $ADBOPTS forward tcp:PORT tcp:22
echo with a proper PORT, e.g.\; 8888
echo i.e.\; adb $ADBOPTS forward tcp:8888 tcp:22
echo then ssh phablet@localhost -p 8888
fi