# Copyright 2018 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file.
import logging import os import subprocess import threading
class CommandRunner(object): """Helper class used to execute commands on a remote host over SSH."""
def __init__(self, config_path, host, port): """Creates a CommandRunner that connects to the specified |host| and |port|
using the ssh config at the specified |config_path|.
config_path: Full path to SSH configuration.
host: The hostname or IP address of the remote host.
port: The port to connect to."""
self._config_path = config_path
self._host = host
self._port = port
def RunCommand(self, command, silent, timeout_secs=None): """Executes an SSH command on the remote host and blocks until completion.
command: A list of strings containing the command and its arguments.
silent: Iftrue, suppresses all output from'ssh'.
timeout_secs: If set, limits the amount of time that |command| may run.
Commands which exceed the timeout are killed.
def RunCommandPiped(self, command, stdout, stderr, ssh_args = None, **kwargs): """Executes an SSH command on the remote host and returns a process object with access to the command's stdio streams. Does not block.
command: A list of strings containing the command and its arguments.
stdout: subprocess stdout. Must not be None.
stderr: subprocess stderr. Must not be None.
ssh_args: Arguments that will be passed to SSH.
kwargs: A dictionary of parameters to be passed to subprocess.Popen().
The parameters can be used to override stdin and stdout, for
example.
Returns a Popen object for the command."""
ifnot stdout ornot stderr: raise Exception('Stdout/stderr must be specified explicitly')
def RunScp(self, sources, dest, direction, recursive=False): """Copies a file to or from a remote host using SCP and blocks until
completion.
sources: Paths of the files to be copied.
dest: The path that |source| will be copied to.
direction: Indicates whether the file should be copied to orfrom the remote side.
Valid values are COPY_TO_TARGET or COPY_FROM_TARGET.
recursive: Iftrue, performs a recursive copy.
Function will raise an assertion if a failure occurred."""
scp_command = _SCP[:] if _SSH_LOGGER.getEffectiveLevel() == logging.DEBUG:
scp_command.append('-v') if recursive:
scp_command.append('-r')
host = _EscapeIfIPv6Address(self._host)
if direction == COPY_TO_TARGET:
dest = "%s:%s" % (host, dest) else:
sources = ["%s:%s" % (host, source) for source in sources]
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.