Skip to content

Instantly share code, notes, and snippets.

@komeiji-satori
Created April 25, 2018 06:58
Show Gist options
  • Save komeiji-satori/eef10fcbaf1301b7e8db8192788a5792 to your computer and use it in GitHub Desktop.
Save komeiji-satori/eef10fcbaf1301b7e8db8192788a5792 to your computer and use it in GitHub Desktop.
<?php
function remote_exec($host = null, $port = null, $auth = [], $commands = [])
{
$result = [];
$connection = ssh2_connect($host, $port);
if ($auth["type"] == "pubkey") {
ssh2_auth_pubkey_file($connection, $auth["username"], $auth["public_key"], $auth["private_key"], $auth["password"]);
} else {
ssh2_auth_password($connection, $auth["username"], $auth["password"]);
}
foreach ($commands as $command) {
$stream = ssh2_exec($connection, $command);
stream_set_blocking($stream, true);
$result[] = stream_get_contents($stream);
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment