Cradle  0.3.3
Simple library for creating Web-based applications
Ssh Class Reference

SSH interaction. More...

Inheritance diagram for Ssh:
RemoteExecutor

Data Fields

const ERR_STREAM_CREATE = 5
 
const EXECTYPE_EXEC = 1
 
const EXECTYPE_SHELL = 2
 
 $port = 22
 
 $execType = self::EXECTYPE_EXEC
 
 $terminalType = 'vt102'
 
 $width = 80
 
 $height = 25
 
 $widthHeightType = SSH2_TERM_UNIT_CHARS
 
 $environment
 
- Data Fields inherited from RemoteExecutor
const ERR_HOST_IS_EMPTY = 1
 
const ERR_UNABLE_TO_CONNECT = 2
 
const ERR_AUTH_FAIL = 3
 
 $host
 
 $port
 
 $user
 
 $password
 
 $timeout
 
 $errorSilent = true
 
 $debug = 1
 
 $error = 1
 

Protected Member Functions

 error ($code, $source="", $detail=null)
 
 connect ()
 
 authenticate ()
 
 prepareCommand ($command)
 
 executeCommand ($command, $timeout=null)
 
- Protected Member Functions inherited from RemoteExecutor
 init ($config)
 
 error ($code, $source="", $message=null, $detail=null)
 
 debug ($message, $source="")
 
 connect ()
 
 authenticate ()
 
 prepareCommand ($command)
 
 executeCommand ($command, $timeout=null)
 

Additional Inherited Members

- Public Member Functions inherited from RemoteExecutor
 __construct ($config=null)
 
 __destruct ()
 
 open ($config=null)
 
 close ()
 
 exec ($command, $timeout=null)
 
 getLastError ()
 
 getErrors ()
 
 getDebug ()
 
 clearBuffers ($type= 'all')
 
 isAlive ($timeout=null)
 
- Protected Attributes inherited from RemoteExecutor
 $connection
 
 $isAuthenticated = false
 
 $debugLogger
 
 $errorLogger
 

Detailed Description

SSH interaction.

A simple class to send commands to remote host through the SSH protocol.

Version
4.0
Author
Digger mrdig.nosp@m.ger@.nosp@m.sad-s.nosp@m.yste.nosp@m.ms.ru

Example of usage:

<?php
require_once __DIR__ . '/../../../../autoload.php';
//--- 1. Short usage:
$r = (new Ssh([ 'host' => "host", 'user' => "user", 'password' => "password" ]))->exec("hostname");
print_r( $r );
//--- 2. Normal usage:
$t = new Ssh([
'host' => "host",
'user' => "username",
'password' => "password",
'debug' => "debug.dat", // to file (option)
]);
$commands = "hostname"; // Single command as a string
$commands = "hostname\ndate"; // Several commands separated by "\n"
$commands = ["hostname"]; // Single command as an array
$commands = ["hostname", "date"]; // Several commands as an array
$r = $t->exec($commands);
print_r( $r );
if ($r === false) {
echo "Error: " . implode(" : ", $t->getLastError()) . "\n";
print_r($t->getErrors());
}
//--- 3. Advanced usage:
$t = new Ssh([
'host' => "host",
'user' => "username",
'password' => "password",
'debug' => 2, // to STDIN (echo)
'errorSilent' => false, // enable exceptions
]);
$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);
}
} catch (Exception $e) {
echo "Exception: " . $e->getMessage() . " Code: " . $e->getCode() . "\n";
echo "Error: " . implode(" : ", $t->getLastError()) . "\n";
print_r($t->getErrors());
}
$t->close();
//--- 4. Several targets:
$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);
} catch (Exception $e) {
echo "Exception: " . $e->getMessage() . " Code: " . $e->getCode() . "\n";
echo "Error: " . implode(" : ", $t->getLastError()) . "\n";
print_r($t->getErrors());
}
}
$t->close();

Member Function Documentation

authenticate ( )
protected

Authenticate the access to remote side

Returns
boolean TRUE - Authentication is success.
FALSE - Authentication is fail.
connect ( )
protected

Connect to remote host Method to override

Returns
boolean TRUE - Successfuly connected.
FALSE - Connection is fail.
error (   $code,
  $source = "",
  $detail = null 
)
protected

Create an error

Exceptions
Exception
executeCommand (   $command,
  $timeout = null 
)
protected

Execute a single command on remote side

Returns
string The text data of response.
prepareCommand (   $command)
protected

Prepare commands to execute

Parameters
string$commandA string with commands separated by "\n"
Returns
array An array of commands

Field Documentation

$environment

string An associative array of name/value pairs to set in the target environment (option).

$execType = self::EXECTYPE_EXEC

int The type of remote interaction. Acceptable values: self::EXECTYPE_EXEC | self::EXECTYPE_SHELL

See also
EXECTYPE_EXEC
EXECTYPE_SHELL
$height = 25

int Height of the virtual terminal.

$port = 22

int The target TCP port.

$terminalType = 'vt102'

string Type of virtual terminal (option).

$width = 80

int Width of the virtual terminal.

$widthHeightType = SSH2_TERM_UNIT_CHARS

int should be one of SSH2_TERM_UNIT_CHARS or SSH2_TERM_UNIT_PIXELS

const EXECTYPE_EXEC = 1

Execute a command on a remote server

const EXECTYPE_SHELL = 2

Request an interactive shell


The documentation for this class was generated from the following file: