~popey/phablet-tools/fix-1360582

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
#!/bin/sh -e
# 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) 2014 Canonical, Ltd.

usage () {
cat <<EOF
Usage: $0 [-s SERIAL]

This tool will start ssh on your connected Ubuntu Touch device, forward a local
port to the device, copy your ssh id down to the device (so you only have to log
in once), rsync down your bash config so you get any nice prompt & aliases you
might be used to from your host bash config, and then finally sshs into the
device through the locally forwarded port.

This results in a very nice shell, which for example can display the output of
'top' at the correct terminal size, rather than being stuck at 80x25 like 'adb
shell'

Like ssh-copy-id, this script will push down the newest ssh key it can find in
~/.ssh/*.pub, so if you find the wrong key being pushed down, simply use 'touch'
to make your desired key the newest one, and then this script will find it.
EOF
exit 1
}

if [ -f "$(dirname $0)/shell-adb-common.sh" ]; then
    . "$(dirname $0)/shell-adb-common.sh"
else
    . "/usr/share/phabletutils/shell-adb-common.sh"
fi

ARGS=$(getopt -o hs: -n "$0" -- "$@")
if [ $? -ne 0 ] ; then
    usage
fi
eval set -- "$ARGS"

while true; do
    case "$1" in
        -h)
            usage
            ;;
        -s)
            export ANDROID_SERIAL="$2"
            shift 2
            break
            ;;
        *)
            break
            ;;
    esac
done

check_devices

# Start ssh on the device and use port forwarding to connect to it.
# This means that we'll connect to the device through the USB cable
# and won't depend on if the device has it's wifi configured or not.
adb shell start ssh
for PORT in `seq 2222 2299`; do
    adb forward tcp:$PORT tcp:22 && break
done

SSH_OPTS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p $PORT"

# Purge the device host key so that SSH doesn't print a scary warning about it
# (it changes every time the device is reflashed and this is expected)
ssh-keygen -f ~/.ssh/known_hosts -R [localhost]:$PORT

# Copy your ssh id down to the device so you never need a password.
NEWEST_KEY=$(ls -t ~/.ssh/*.pub | grep -v -- -cert.pub | head -1)
adb push $NEWEST_KEY /home/phablet/.ssh/authorized_keys
adb shell chown phablet:phablet -R /home/phablet/.ssh/
adb shell chmod 700 /home/phablet/.ssh
adb shell chmod 600 /home/phablet/.ssh/authorized_keys

# Copy your bash config down to the device so you get the benefit of your
# colourful $PS1 prompt and any bash aliases that you may be used to from your
# host device.
rsync -qae "ssh $SSH_OPTS" ~/.bash* ~/.profile* phablet@localhost:/home/phablet

# Now connect to the device and provide the user with a shell.
ssh $SSH_OPTS phablet@localhost