Recommend this page to a friend! |
Download |
Info | Documentation | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
79% | Total: 400 | All time: 6,572 This week: 43 |
Version | License | PHP version | Categories | |||
ssh-session 1.0.9 | GNU General Publi... | 5.5 | Networking, PHP 5, Files and Folders |
Description | Author | |||
This package can run arbitrary length commands in a server with SSH. Innovation Award
|
The SshSession class (and all of its related classes) is intended to establish SSH connections to a remote host running an sshd server and allow you to execute commands remotely and retrieve their output, or access the remote filesystem using the SFTP protocol.
The class relies on the libssh2 PHP extension.
The following example illustrates a simple usage of this package ; it connects remotely, executes the "ls -al" command by redirectoring its output to file /tmp/ls.out, then retrieves file contents and display them :
require_once ( 'Session.phpclass' ) ;
// Create an instance for host 1.2.3.4 port 22 ; no connection exists at that stage
$session = new SshSession ( '1.2.3.4', 22 ) ;
// Authenticate using the specified user/password
$session -> Authenticate ( new SshPasswordAuthentication ( $session, 'myuser', 'mypassword' ) ;
// Execute the ls command and redirect its output to file /tmp/ls.out
$status = $session -> Execute ( "ls -al / >tmp/ls.out' ) ;
// Get a reference object to the remote filesystem
$fs = $session -> GetFileSystem ( ) ;
// Retrieve then display /tmp/ls.out file contents
echo $fs -> file_get_contents ( '/tmp/ls.out' ) ;
// Remove temporary file
$fs -> unlink ( '/tmp/ls.out' ) ;
Of course, you can also use authentication using public and private keys defined in external files ; just replace the following code :
$session -> Authenticate ( new SshPasswordAuthentication ( $session, 'myuser', 'mypassword' ) ;
with :
$session -> Authenticate ( new SshPublicKeyAuthentication ( $session, 'myuser', 'mypublickey.ppk', 'myprivatekey.ppk' ) ;
The SshSession class tries to protect you from some issues found with the libssh2 PHP extension (especially on Windows) and from some limitations of the SSH protocol :
At least on Windows systems, the following two cases will bring you an error : - Execute a command, then an SFTP operation, then another command - Execute two commands consecutively
The SshSession class handles the burden of such situations by automatically disconnecting you then reconnecting before the next operation to be performed, in order to avoid you to have to deal with such errors.
The remote systems you will be accessing will need the following : - An up-and-running sshd server - A version of PHP with the libssh2 library installed and configured as an extension library (either in CLI mode or when running with a web server) - If you want to authenticate using public and private keys, the user on your remote server will need to have your public key in the authorized_keys file of the .ssh directory located in the home directory for this user.
PHP 5.6.28 broke something (or radically changed something) in the way the ssh2.sftp wrapper parses a file specification. This led to errors when using the opendir() function, for example.
This became "less worse" with PHP 7.0, but resource id strings in path specifications remain unrecognized, unlike in versions <= 5.6.27 (the Ssh Session classes has been fixed to handle this new case).
PHP 7.1 solved the problem ; however, on Windows platforms, using multiple SFTP sessions within the same script can lead to errors when trying to access existing remote files. More annoying is that an access violation is generated at the end of the script in php_libssh2.dll. This problem, which is linked to the current implementation of the libssh2 extension, is currently under investigation.
This package contains 4 main classes :
The classes you are likely to use most often will probably be SshPasswordAuthentication and SshPublicKeyAuthentication.
Builds an Ssh session, including access to shell and sftp.
Note that instantiating an SshSession object does not establish a connection to the remote system : you must call the Connect() method before that.
The parameters are the following :
Authenticates on an already connected session using the specified SshAuthentication class-derived object.
Connects to the specified remote host. See the class constructor help for an explanation about the method parameters.
Returns an SshConnection object.
Disconnects from an existing SSH session.
Returns true if disconnection was successful, false otherwise (for example, when no connection was established).
Executes a command on a remote server.
The parameters are the following :
Returns the status of the last executed command (the $? variable value of the bash/sh shell interpreter).
_NOTES_ :
For those reasons, the Execute() method transforms the supplied input commands to the following script :
bash 2>&1 <<END
$commands
END
The drawback is that stderr is interspersed with stdout, but in the order it has been output.
Creates an SshFileSystem object (if not already created) to allow remote access to the server files.
Disconnects, reconnects then re-authenticates.
Added mainly as a workaround for the libssh2 bug which prevents ANYTHING to be executed or transferred after a command has been executed (apparently, it concerns only Windows implementations).
Returns true if the session object has established a connection to the remote server.
Note that being connected does not necessarily mean being authenticated.
Returns true if the connection is opened and a user has been authenticated.
Returns an SshConnection object.
An object of type SshFileSystem provides access to a remote file system.
It can be retrieved using the GetFileSystem() method of the SshSession class, once connected and authenticated (an exception is thrown if no connection has been established or if no user has been authenticated).
Most of the methods of this class mimic their classic PHP counterpart.
Returns a path using the ssh2.sftp:// stream wrapper.
Changes the current working directory on the remote server and returns the previous one
Changes the permissions for the specified file on the remote server. See the chmod() function for an explanation on the $mode parameter.
Closes a directory opened by the opendir() method.
Closes a file opened by the fopen() method.
Reads a line from the specified file opened by the fopen() method.
Gets remote file contents, as an array of lines.
Checks if the specified file exists on the remote host.
Retrieves the contents of a remote file, using the ssh2.sftp wrapper.
Note that the $maxlen parameter of the traditional PHP file\_get\_contents function is not used, since it always gives empty results.
Replaces the contents of a remote file, using the ssh2.sftp wrapper.
Opens a file on the remote host.
Reads an already opened file until the end and returns its data.
fprintf() function, on a remote file.
fputs() function, on a remote file.
fread() function, on a remote file.
Seeks on a remote file.
Returns the current offset of remote file.
fwrite() function, on a remote file.
Returns the current working directory, which is evaluated through the realpath() method upon connection.
Closes an opened gzipped file.
Opens a remote gzipped file.
gzread() function, on a remote file.
Checks if the specified file exists on the remote host and is a directory.
Checks if the specified file exists on the remote host and is a plain file.
Stats a remote symbolic link target.
Creates a directory on the remote host.
Opens a remote directory.
Reads the next directory entry.
Returns the target path of a symbolic link on the remote host.
Returns the real path of a file on the remote host.
Receives a file from the remote server.
Renames a file on the remote host.
Rewinds an already opened file on the remote server.
Removes a directory from the remote host.
Sends a file to the remote server.
Stats a remote file.
Creates a symbolic link.
Deletes a file from the remote host.
Returns the SshSession parent object for this file system.
A resource returned by the PHP ssh2\_sftp() function.
Current working directory on the remote server.
An object of a type belonging to one of the classes derived from SshAuthentication needs to be supplied to the Connect() method. This section describes the constructors of these various classes.
Use this class when you need to authenticate using a user and password combination ; the constructor has the following form :
__construct ( $session, $username, $password ) ;
Where :
Use this class when you need to authenticate using public/private keys ; the constructor has the following form :
__construct ( $session, $username, $public_key_file, $private_key_file, $passphrase = null )
The parameters are the following :
This class has not been tested yet.
Use this class when you need to authenticate using public/private keys that are located on a third-party server.
The constructor has the following form :
__construct ( $session, $username, $host, $public_key_file, $private_key_file, $passphrase = null ) ;
This class has not been tested yet.
The constructor has the following form :
__construct ( $session, $username ) ;
This class has not been tested yet.
The constructor has the following form :
__construct ( $session, $username ) ;
You have to edit the file example.inc.php ; it contains variables that you MUST set to your own configuration needs before running the examples :
If you used puttygen to generate your public and private keys, you have to know that the key that is labelled :
Public key for pasting into OpenSSH authorized_keys file
will not have the correct format for using it as a public key on Unix systems. You will have to go to the Conversions menu and chose the "Export OpenSSH key" option.
Files (11) |
File | Role | Description |
---|---|---|
Authentication.phpclass | Class | Class source |
Connection.phpclass | Class | Class source |
example.inc.php | Conf. | Configuration script |
example.key.php | Example | Example script |
example.password.php | Example | Example script |
FileSystem.phpclass | Class | Class source |
LICENSE | Lic. | License text |
NOTICE | Data | Auxiliary data |
README.examples.md | Doc. | Documentation |
README.md | Doc. | Documentation |
Session.phpclass | Class | Class source |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
User Ratings | User Comments (1) | ||||||||||||||||||||||||||||||||||
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.