public function testParseSSHUrl() { $testcase = 'foo'; $this->assertEquals(array('host' => 'foo'), Utils\parse_ssh_url($testcase)); $this->assertEquals('foo', Utils\parse_ssh_url($testcase, PHP_URL_HOST)); $this->assertEquals(null, Utils\parse_ssh_url($testcase, PHP_URL_PORT)); $this->assertEquals(null, Utils\parse_ssh_url($testcase, PHP_URL_PATH)); $testcase = 'foo.com'; $this->assertEquals(array('host' => 'foo.com'), Utils\parse_ssh_url($testcase)); $this->assertEquals('foo.com', Utils\parse_ssh_url($testcase, PHP_URL_HOST)); $this->assertEquals(null, Utils\parse_ssh_url($testcase, PHP_URL_PORT)); $this->assertEquals(null, Utils\parse_ssh_url($testcase, PHP_URL_PATH)); $testcase = 'foo.com:2222'; $this->assertEquals(array('host' => 'foo.com', 'port' => 2222), Utils\parse_ssh_url($testcase)); $this->assertEquals('foo.com', Utils\parse_ssh_url($testcase, PHP_URL_HOST)); $this->assertEquals(2222, Utils\parse_ssh_url($testcase, PHP_URL_PORT)); $this->assertEquals(null, Utils\parse_ssh_url($testcase, PHP_URL_PATH)); $testcase = 'foo.com:2222/path/to/dir'; $this->assertEquals(array('host' => 'foo.com', 'port' => 2222, 'path' => '/path/to/dir'), Utils\parse_ssh_url($testcase)); $this->assertEquals('foo.com', Utils\parse_ssh_url($testcase, PHP_URL_HOST)); $this->assertEquals(2222, Utils\parse_ssh_url($testcase, PHP_URL_PORT)); $this->assertEquals('/path/to/dir', Utils\parse_ssh_url($testcase, PHP_URL_PATH)); $testcase = 'foo.com~/path/to/dir'; $this->assertEquals(array('host' => 'foo.com', 'path' => '~/path/to/dir'), Utils\parse_ssh_url($testcase)); $this->assertEquals('foo.com', Utils\parse_ssh_url($testcase, PHP_URL_HOST)); $this->assertEquals(null, Utils\parse_ssh_url($testcase, PHP_URL_PORT)); $this->assertEquals('~/path/to/dir', Utils\parse_ssh_url($testcase, PHP_URL_PATH)); // No host $testcase = '~/path/to/dir'; $this->assertEquals(array(), Utils\parse_ssh_url($testcase)); $this->assertEquals(null, Utils\parse_ssh_url($testcase, PHP_URL_HOST)); $this->assertEquals(null, Utils\parse_ssh_url($testcase, PHP_URL_PORT)); $this->assertEquals(null, Utils\parse_ssh_url($testcase, PHP_URL_PATH)); }
/** * Perform a command against a remote server over SSH */ private function run_ssh_command($ssh) { WP_CLI::do_hook('before_ssh'); // host OR host/path/to/wordpress OR host:port/path/to/wordpress $bits = Utils\parse_ssh_url($ssh); $host = isset($bits['host']) ? $bits['host'] : null; $port = isset($bits['port']) ? $bits['port'] : null; $path = isset($bits['path']) ? $bits['path'] : null; WP_CLI::debug('SSH host: ' . $host, 'bootstrap'); WP_CLI::debug('SSH port: ' . $port, 'bootstrap'); WP_CLI::debug('SSH path: ' . $path, 'bootstrap'); $is_tty = function_exists('posix_isatty') && posix_isatty(STDOUT); $pre_cmd = getenv('WP_CLI_SSH_PRE_CMD'); if ($pre_cmd) { $pre_cmd = rtrim($pre_cmd, ';') . '; '; } $wp_binary = 'wp'; $wp_args = array_slice($GLOBALS['argv'], 1); $wp_path = $path ? sprintf('--path=%s', str_replace('~', '$HOME', $path)) : ''; if ($this->alias && !empty($wp_args[0]) && $this->alias === $wp_args[0]) { array_shift($wp_args); } foreach ($wp_args as $k => $v) { if (preg_match('#--ssh=#', $v)) { unset($wp_args[$k]); } } $unescaped_command = sprintf('ssh -q %s%s %s %s', $port ? '-p ' . (int) $port . ' ' : '', $host, $is_tty ? '-t' : '-T', $pre_cmd . $wp_binary . ' ' . $wp_path . ' ' . implode(' ', array_map('escapeshellarg', $wp_args))); WP_CLI::debug('Running SSH command: ' . $unescaped_command, 'bootstrap'); $escaped_command = sprintf('ssh -q %s%s %s %s', $port ? '-p ' . (int) $port . ' ' : '', escapeshellarg($host), $is_tty ? '-t' : '-T', escapeshellarg($pre_cmd . $wp_binary . ' ' . $wp_path . ' ' . implode(' ', array_map('escapeshellarg', $wp_args)))); passthru($escaped_command, $exit_code); if (255 === $exit_code) { WP_CLI::error('Cannot connect over SSH using provided configuration.', 255); } else { exit($exit_code); } }