WP_CLI
Runner::run_ssh_command
Perform a command against a remote server over SSH (or a container using scheme of "docker", "docker-compose", or "docker-compose-run").
Method of the class: Runner{}
No Hooks.
Returns
null. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->run_ssh_command( $connection_string );
- $connection_string(string) (required)
- Passed connection string.
Runner::run_ssh_command() Runner::run ssh command code WP-CLI 2.13.0-alpha
private function run_ssh_command( $connection_string ) {
WP_CLI::do_hook( 'before_ssh' );
$bits = Utils\parse_ssh_url( $connection_string );
$pre_cmd = getenv( 'WP_CLI_SSH_PRE_CMD' );
if ( $pre_cmd ) {
WP_CLI::warning( "WP_CLI_SSH_PRE_CMD found, executing the following command(s) on the remote machine:\n $pre_cmd" );
$pre_cmd = rtrim( $pre_cmd, ';' ) . '; ';
}
$env_vars = '';
if ( getenv( 'WP_CLI_STRICT_ARGS_MODE' ) ) {
$env_vars .= 'WP_CLI_STRICT_ARGS_MODE=1 ';
}
$wp_binary = getenv( 'WP_CLI_SSH_BINARY' ) ?: 'wp';
$wp_args = array_slice( $GLOBALS['argv'], 1 );
if ( $this->alias && ! empty( $wp_args[0] ) && $this->alias === $wp_args[0] ) {
array_shift( $wp_args );
$runtime_alias = [];
foreach ( $this->aliases[ $this->alias ] as $key => $value ) {
if ( 'ssh' === $key ) {
continue;
}
$runtime_alias[ $key ] = $value;
}
if ( ! empty( $runtime_alias ) ) {
$encoded_alias = json_encode(
[
$this->alias => $runtime_alias,
]
);
$wp_binary = "WP_CLI_RUNTIME_ALIAS='{$encoded_alias}' {$wp_binary} {$this->alias}";
}
}
foreach ( $wp_args as $k => $v ) {
if ( preg_match( '#--ssh=#', $v ) ) {
unset( $wp_args[ $k ] );
}
}
$wp_command = $pre_cmd . $env_vars . $wp_binary . ' ' . implode( ' ', array_map( 'escapeshellarg', $wp_args ) );
if ( isset( $bits['scheme'] ) && 'docker-compose-run' === $bits['scheme'] ) {
$wp_command = implode( ' ', $wp_args );
}
$escaped_command = $this->generate_ssh_command( $bits, $wp_command );
passthru( $escaped_command, $exit_code );
if ( 255 === $exit_code ) {
WP_CLI::error( 'Cannot connect over SSH using provided configuration.', 255 );
} else {
exit( $exit_code );
}
}