Testing script with remote ping
<?php
$connection = ssh2_connect(<IP_ADDRESS>, <PORT>, ['hostkey' => 'ssh-rsa']);
$authenticate = ssh2_auth_pubkey_file(
$connection, <USERNAME>,
"/path/to/.ssh/id_rsa.host.pub",
"/path/to/.ssh/id_rsa.host"
);
$resultErr = $resultIO = "";
if ($authenticate) {
$stream = ssh2_exec($connection, "ping -c 4 -t 15 hk.yahoo.com");
$streamErr = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
$streamIO = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
stream_set_blocking($streamErr, true);
stream_set_blocking($streamIO, true);
$resultErr = stream_get_contents($streamErr);
$resultIO = stream_get_contents($streamIO);
fclose($streamErr);
fclose($streamIO);
}else{
exit("Failed to authenticate");
}
if (empty($resultErr) === true) {
echo "<h3>Ping</h3>";
echo "<pre>",$resultIO,"</pre>";
}else{
echo "<h3>Error</h3>";
echo "<pre>",$resultErr,"</pre>";
}