blob: ac7b5a29812714ff6c046b664ee535bb674790a8 [file] [log] [blame]
# Copyright 2016 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# The purpose of this script is to help user setup SSH/ADB connection to
# the Android apps on their Chromebook. It performs the following steps:
# 1. Download SSH RSA key for devmode Chromebook.
# 2. Add an entry in .ssh/config, then ssh to the Chromebook via RSA key.
# 3. Copy local ADB key to Chromebook so that ADB would accept request from
# this workstation.
# Configurable constants
# Environment checks
if [ $# -ne 2 ]; then
echo "Usage: $0 <ip address> <chromebook host name>"
exit 1
if ! ping -c1 -W1 ${IP} &> /dev/null; then
echo "ERROR: ${IP} is not reachable"
exit 1
# Download SSH key file
mkdir --mode=700 -p ~/.ssh
TESTING_RSA=$(readlink -f ~/.ssh/chromebook_testing_rsa)
if [ ! -f ${TESTING_RSA} ]; then
curl -s ${KEY_URL} | base64 --decode > ${TESTING_RSA}
chmod 600 ${TESTING_RSA}
# Check if the SSH config already had the host
SSH_CONFIG=$(readlink -f ~/.ssh/config)
if [[ -z "$(grep "Host ${CHROMEBOOK}" ${SSH_CONFIG} 2> /dev/null)" ]]; then
# Alter .ssh/config
cat << EOF >> ${SSH_CONFIG}
Hostname ${IP}
User root
IdentityFile ${TESTING_RSA}
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
LogLevel ERROR
# Connect to Chromebook
function ssh_exec() {
ssh ${CHROMEBOOK} $1 || (echo "ERROR: failed to execute command: $1" && exit 1)
ssh_exec "exit"
echo "INFO: SSH connectivity established."
# Copy ADB key
adb kill-server &> /dev/null
adb start-server &> /dev/null
ADB_KEY=$(readlink -f ~/.android/
if [ -z ${ADB_KEY} ]; then
echo "ERROR: cannot locate ADB public key."
exit 1
ssh_exec "android-sh -c 'cat > /data/misc/adb/adb_keys'" < ${ADB_KEY}
ssh_exec "android-sh -c 'restorecon /data/misc/adb/adb_keys'"
ssh_exec "android-sh -c 'setprop persist.sys.usb.config adb'"
SDK_VERSION=`ssh_exec "android-sh -c 'getprop'"`
# 25: Android N SDK version.
if [ "${SDK_VERSION}" -ge 25 ]; then
# In N or later, init.usb.rc is updated so that persist.sys.usb.config
# is propagated to sys.usb.config only on booting.
# So, here manually propagate it so that adbd is triggered to start.
ssh_exec "android-sh -c 'setprop sys.usb.config adb'"
echo "INFO: ADB set up complete."