blob: 1241e193f9d8edb59e4d8d7eb658071a6af56ff0 [file] [log] [blame]
#!/bin/bash
# Copyright 2017 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#### CATEGORY=Device management
### start a remote interactive shell in the target device
## usage: fx shell [-h|--help] [--info | [<ssh flags>] <command>]
##
## Creates an SSH connection with a device and executes a command.
##
## Arguments:
## -h|--help Print out this message.
## --info Show information about SSH keys and the target device and exit.
## <ssh flags> Flags and command are passed to SSH as is. Consult SSH help
## for a list of available flags.
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $?
show_only=false
case $1 in
--info)
show_only=true
shift
;;
-h|--help)
fx-command-help
exit 0
;;
esac
if $show_only; then
echo -n "SSH port: "
get-device-ssh-port
echo -n "SSH device address: "
get-fuchsia-device-addr
echo -n "SSH private key file: "
get-ssh-privkey
echo -n "SSH authorized keys file: "
get-ssh-authkeys
exit 0
fi
get-ssh-privkey > /dev/null
# Warns if key is not available. Result is discarded.
args=()
device_port="$(get-device-ssh-port)" || exit $?
if [[ -n "${device_port}" ]]; then
args+=( "-p" "${device_port}" )
fi
device_addr="$(get-fuchsia-device-addr)" || exit $?
if [[ -z "${device_addr}" ]]; then
# Error output is provided to stderr by get-fuchsia-device-addr
exit 1
fi
args+=( "${device_addr}" )
args+=( "$@" )
# Note: I know there are people who don't like the host-key message, but DO NOT
# apply -q here, it silences error messages and makes network and configuration
# failures much harder to diagnose when helping people. The control master will
# mean you only get one per TCP socket, which is once per newly booted host.
# It's not a huge burden compared to end user support.
fx-command-exec ssh "${args[@]}"