2 namespace Drush\Commands\core;
4 use Drush\Commands\DrushCommands;
5 use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
6 use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
8 class SshCommands extends DrushCommands implements SiteAliasManagerAwareInterface
10 use SiteAliasManagerAwareTrait;
13 * Connect to a Drupal site's server via SSH.
16 * @option cd Directory to change to if Drupal root is not desired (the default).
17 * @optionset_proc_build
18 * @handle-remote-commands
19 * @usage drush @mysite ssh
20 * Open an interactive shell on @mysite's server.
21 * @usage drush @prod ssh ls /tmp
22 * Run "ls /tmp" on @prod site. If @prod is a site list, then ls will be executed on each site.
23 * @usage drush @prod ssh git pull
24 * Run "git pull" on the Drupal root directory on the @prod site.
25 * @aliases ssh,site-ssh
26 * @topics docs:aliases
28 public function ssh(array $args, $options = ['cd' => true])
30 // n.b. we do not escape the first (0th) arg to allow `drush ssh 'ls /path'`
31 // to work in addition to the preferred form of `drush ssh ls /path`.
32 // Supporting the legacy form means that we cannot give the full path to an
33 // executable if it contains spaces.
34 for ($x = 1; $x < count($args); $x++) {
35 $args[$x] = drush_escapeshellarg($args[$x]);
37 $command = implode(' ', $args);
39 $alias = $this->siteAliasManager()->getSelf();
40 if ($alias->isNone()) {
41 throw new \Exception('A site alias is required. The way you call ssh command has changed to `drush @alias ssh`.');
44 // Local sites run their bash without SSH.
45 if (!$alias->isRemote()) {
46 $return = drush_invoke_process('@self', 'core-execute', [$command], ['escape' => false]);
47 return $return['object'];
50 // We have a remote site - build ssh command and run.
53 if (empty($command)) {
58 $cmd = drush_shell_proc_build($alias, $command, $cd, $interactive);
59 $status = drush_shell_proc_open($cmd);
61 throw new \Exception(dt('An error @code occurred while running the command `@command`', ['@command' => $cmd, '@code' => $status]));