SSH2
class SSH2 (View source)
Pure-PHP implementation of SSHv2.
Constants
MASK_CONSTRUCTOR |
|
MASK_CONNECTED |
|
MASK_LOGIN_REQ |
|
MASK_LOGIN |
|
MASK_SHELL |
|
MASK_WINDOW_ADJUST |
|
CHANNEL_EXEC |
|
CHANNEL_SHELL |
|
CHANNEL_SUBSYSTEM |
|
CHANNEL_AGENT_FORWARD |
|
CHANNEL_KEEP_ALIVE |
|
LOG_SIMPLE |
Returns the message numbers |
LOG_COMPLEX |
Returns the message content |
LOG_REALTIME |
Outputs the content real-time |
LOG_REALTIME_FILE |
Dumps the content real-time to a file |
LOG_MAX_SIZE |
Make sure that the log never gets larger than this |
READ_SIMPLE |
Returns when a string matching $expect exactly is found |
READ_REGEX |
Returns when a string matching the regular expression $expect is found |
READ_NEXT |
Returns whenever a data packet is received. Some data packets may only contain a single character so it may be necessary to call read() multiple times when using this option |
Properties
object | $fsock | The Socket Object | |
protected int | $bitmap | Execution Bitmap | |
protected array | $server_channels | Server Channels | |
protected array | $channel_status | Channel Status | |
protected int | $window_size | The Window Size | |
protected array | $window_size_server_to_client | Window size, server to client | |
protected | $timeout | Timeout | |
protected | $curTimeout | Current Timeout | |
protected string|false | $preferred_signature_format | Preferred Signature Format | |
protected array | $auth | Authentication Credentials |
Methods
Default Constructor.
Set Crypto Engine Mode
Send Identification String First
Send Identification String Last
Send SSH_MSG_KEXINIT First
Send SSH_MSG_KEXINIT Last
Login
Login Helper
Set Timeout
Set Keep Alive
Get the output from stdError
Execute Command
Request agent forwarding of remote server
Returns the output of an interactive shell
Inputs a command into an interactive shell.
Start a subsystem.
Stops a subsystem.
Closes a channel
Is timeout?
Disconnect
Destructor.
Is the connection still active?
Have you successfully been logged in?
Pings a server connection, or tries to reconnect if the connection has gone down
Resets a connection for re-use
Enable Quiet Mode
Disable Quiet Mode
Returns whether Quiet Mode is enabled or not
Enable request-pty when using exec()
Disable request-pty when using exec()
Returns whether request-pty is enabled or not
Gets channel data
Sends Binary Packets
Sends channel data
Disconnect
Define Array
Returns a log of the packets that have been sent and received.
Formats a log for printing
Returns all errors
Returns the last error
Return the server identification.
Returns a list of algorithms the server supports
Returns a list of KEX algorithms that phpseclib supports
Returns a list of host key algorithms that phpseclib supports
Returns a list of symmetric key algorithms that phpseclib supports
Returns a list of MAC algorithms that phpseclib supports
Returns a list of compression algorithms that phpseclib supports
Return list of negotiated algorithms
Allows you to set the terminal
Accepts an associative array with up to four parameters as described at https://www.php.net/manual/en/function.ssh2-connect.php
Returns the banner message.
Returns the server public host key.
Returns the exit status of an SSH command or false.
Returns the number of columns for the terminal window size.
Returns the number of rows for the terminal window size.
Sets the number of columns for the terminal window size.
Sets the number of rows for the terminal window size.
Sets the number of columns and rows for the terminal window size.
To String Magic Method
Get Resource ID
Return existing connection
Return all excising connections
Return the list of authentication methods that may productively continue authentication.
Details
SSH2|void
__construct(mixed $host, int $port = 22, int $timeout = 10)
Default Constructor.
$host can either be a string, representing the host, or a stream resource.
static
setCryptoEngine(int $engine)
Set Crypto Engine Mode
Possible $engine values: OpenSSL, mcrypt, Eval, PHP
sendIdentificationStringFirst()
Send Identification String First
https://tools.ietf.org/html/rfc4253#section-4.2 says "when the connection has been established, both sides MUST send an identification string". It does not say which side sends it first. In theory it shouldn't matter but it is a fact of life that some SSH servers are simply buggy
sendIdentificationStringLast()
Send Identification String Last
https://tools.ietf.org/html/rfc4253#section-4.2 says "when the connection has been established, both sides MUST send an identification string". It does not say which side sends it first. In theory it shouldn't matter but it is a fact of life that some SSH servers are simply buggy
sendKEXINITFirst()
Send SSH_MSG_KEXINIT First
https://tools.ietf.org/html/rfc4253#section-7.1 says "key exchange begins by each sending sending the [SSH_MSG_KEXINIT] packet". It does not say which side sends it first. In theory it shouldn't matter but it is a fact of life that some SSH servers are simply buggy
sendKEXINITLast()
Send SSH_MSG_KEXINIT Last
https://tools.ietf.org/html/rfc4253#section-7.1 says "key exchange begins by each sending sending the [SSH_MSG_KEXINIT] packet". It does not say which side sends it first. In theory it shouldn't matter but it is a fact of life that some SSH servers are simply buggy
bool
login(string $username, string|AsymmetricKey|array[]|Agent|null ...$args)
Login
The $password parameter can be a plaintext password, a \phpseclib3\Crypt\RSA|EC|DSA object, a \phpseclib3\System\SSH\Agent object or an array
protected bool
sublogin(string $username, string[] ...$args)
Login Helper
setTimeout(mixed $timeout)
Set Timeout
$ssh->exec('ping 127.0.0.1'); on a Linux host will never return and will run indefinitely. setTimeout() makes it so it'll timeout. Setting $timeout to false or 0 will mean there is no timeout.
setKeepAlive(int $interval)
Set Keep Alive
Sends an SSH2_MSG_IGNORE message every x seconds, if x is a positive non-zero number.
getStdError()
Get the output from stdError
string
exec(string $command, callable $callback = null)
Execute Command
If $callback is set to false then \phpseclib3\Net\SSH2::get_channel_packet(self::CHANNEL_EXEC) will need to be called manually. In all likelihood, this is not a feature you want to be taking advantage of.
bool
requestAgentForwarding()
Request agent forwarding of remote server
string|bool|null
read(string $expect = '', int $mode = self::READ_SIMPLE)
Returns the output of an interactive shell
Returns when there's a match for $expect, which can take the form of a string literal or, if $mode == self::READ_REGEX, a regular expression.
bool
write(string $cmd)
Inputs a command into an interactive shell.
bool
startSubsystem(string $subsystem)
Start a subsystem.
Right now only one subsystem at a time is supported. To support multiple subsystem's stopSubsystem() could accept a string that contained the name of the subsystem, but at that point, only one subsystem of each type could be opened. To support multiple subsystem's of the same name maybe it'd be best if startSubsystem() generated a new channel id and returns that and then that that was passed into stopSubsystem() but that'll be saved for a future date and implemented if there's sufficient demand for such a feature.
bool
stopSubsystem()
Stops a subsystem.
reset()
Closes a channel
If read() timed out you might want to just close the channel and have it auto-restart on the next read() call
isTimeout()
Is timeout?
Did exec() or read() return because they timed out or because they encountered the end?
disconnect()
Disconnect
__destruct()
Destructor.
Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call disconnect().
bool
isConnected()
Is the connection still active?
bool
isAuthenticated()
Have you successfully been logged in?
bool
ping()
Pings a server connection, or tries to reconnect if the connection has gone down
Inspired by http://php.net/manual/en/mysqli.ping.php
protected
reset_connection(int $reason)
Resets a connection for re-use
enableQuietMode()
Enable Quiet Mode
Suppress stderr from output
disableQuietMode()
Disable Quiet Mode
Show stderr in output
bool
isQuietModeEnabled()
Returns whether Quiet Mode is enabled or not
enablePTY()
Enable request-pty when using exec()
disablePTY()
Disable request-pty when using exec()
bool
isPTYEnabled()
Returns whether request-pty is enabled or not
protected mixed
get_channel_packet(int $client_channel, bool $skip_extended = false)
Gets channel data
Returns the data as a string. bool(true) is returned if:
- the server closes the channel
- if the connection times out
- if the channel status is CHANNEL_OPEN and the response was CHANNEL_OPEN_CONFIRMATION
- if the channel status is CHANNEL_REQUEST and the response was CHANNEL_SUCCESS
bool(false) is returned if:
- if the channel status is CHANNEL_REQUEST and the response was CHANNEL_FAILURE
protected bool
send_binary_packet(string $data, string $logged = null)
Sends Binary Packets
See '6. Binary Packet Protocol' of rfc4253 for more info.
protected bool
send_channel_packet(int $client_channel, string $data)
Sends channel data
Spans multiple SSH_MSG_CHANNEL_DATAs if appropriate
protected bool
disconnect_helper(int $reason)
Disconnect
protected
define_array(array ...$args)
Define Array
Takes any number of arrays whose indices are integers and whose values are strings and defines a bunch of named constants from it, using the value as the name of the constant and the index as the value of the constant. If any of the constants that would be defined already exists, none of the constants will be defined.
array|false|string
getLog()
Returns a log of the packets that have been sent and received.
Returns a string if NET_SSH2_LOGGING == self::LOG_COMPLEX, an array if NET_SSH2_LOGGING == self::LOG_SIMPLE and false if !defined('NET_SSH2_LOGGING')
protected string
format_log(array $message_log, array $message_number_log)
Formats a log for printing
string[]
getErrors()
Returns all errors
string
getLastError()
Returns the last error
string
getServerIdentification()
Return the server identification.
array
getServerAlgorithms()
Returns a list of algorithms the server supports
static array
getSupportedKEXAlgorithms()
Returns a list of KEX algorithms that phpseclib supports
static array
getSupportedHostKeyAlgorithms()
Returns a list of host key algorithms that phpseclib supports
static array
getSupportedEncryptionAlgorithms()
Returns a list of symmetric key algorithms that phpseclib supports
static array
getSupportedMACAlgorithms()
Returns a list of MAC algorithms that phpseclib supports
static array
getSupportedCompressionAlgorithms()
Returns a list of compression algorithms that phpseclib supports
array
getAlgorithmsNegotiated()
Return list of negotiated algorithms
Uses the same format as https://www.php.net/ssh2-methods-negotiated
setTerminal(string $term)
Allows you to set the terminal
setPreferredAlgorithms(array $methods)
Accepts an associative array with up to four parameters as described at https://www.php.net/manual/en/function.ssh2-connect.php
string
getBannerMessage()
Returns the banner message.
Quoting from the RFC, "in some jurisdictions, sending a warning message before authentication may be relevant for getting legal protection."
mixed
getServerPublicHostKey()
Returns the server public host key.
Caching this the first time you connect to a server and checking the result on subsequent connections is recommended. Returns false if the server signature is not signed correctly with the public host key.
false|int
getExitStatus()
Returns the exit status of an SSH command or false.
int
getWindowColumns()
Returns the number of columns for the terminal window size.
int
getWindowRows()
Returns the number of rows for the terminal window size.
setWindowColumns(int $value)
Sets the number of columns for the terminal window size.
setWindowRows(int $value)
Sets the number of rows for the terminal window size.
setWindowSize(int $columns = 80, int $rows = 24)
Sets the number of columns and rows for the terminal window size.
string
__toString()
To String Magic Method
string
getResourceId()
Get Resource ID
We use } because that symbols should not be in URL according to {@link http://tools.ietf.org/html/rfc3986#section-2 RFC}. It will safe us from any conflicts, because otherwise regexp will match all alphanumeric domains.
static bool|SSH2
getConnectionByResourceId(string $id)
Return existing connection
static SSH2[]
getConnections()
Return all excising connections
array|null
getAuthMethodsToContinue()
Return the list of authentication methods that may productively continue authentication.