SSH interaction.
A simple class to send commands to remote host through the SSH protocol.
<?php
require_once __DIR__ . '/../../../../autoload.php';
$r = (
new Ssh([
'host' =>
"host",
'user' =>
"user",
'password' =>
"password" ]))->
exec(
"hostname");
print_r( $r );
$t = new Ssh([
'host' => "host",
'user' => "username",
'password' => "password",
'debug' => "debug.dat",
]);
$commands = "hostname";
$commands = "hostname\ndate";
$commands = ["hostname"];
$commands = ["hostname", "date"];
$r = $t->exec($commands);
print_r( $r );
if ($r === false) {
echo "Error: " . implode(" : ", $t->getLastError()) . "\n";
print_r($t->getErrors());
}
$t = new Ssh([
'host' => "host",
'user' => "username",
'password' => "password",
'debug' => 2,
'errorSilent' => false,
]);
$commands = ["hostname", "date"];
if (!is_array($commands)) { $commands = [$cmd]; }
try {
foreach ($commands as $command) {
echo "command : $command\n";
$r = $t->exec($command);
print_r($r);
}
echo "Exception: " . $e->getMessage() . " Code: " . $e->getCode() . "\n";
echo "Error: " . implode(" : ", $t->getLastError()) . "\n";
print_r($t->getErrors());
}
$t->close();
$config = ['user' => "user", 'password' => "password", 'errorSilent' => false, 'debug' => 2];
$hosts = ['host1', 'host2', 'host3'];
$commands = ['hostname', 'date'];
$t = new Ssh($config);
foreach ($hosts as
$host) {
try {
echo "Host: $host\n";
$t->open($host);
$r = $t->exec($commands);
print_r($r);
echo "Exception: " . $e->getMessage() . " Code: " . $e->getCode() . "\n";
echo "Error: " . implode(" : ", $t->getLastError()) . "\n";
print_r($t->getErrors());
}
}
$t->close();