SSH
MMO\core\server · 1 symbol · generated 30 Sep 2026
SSH
SSH.php:18A remote shell over SSH (wraps the ssh2 PECL extension).
- Namespace
MMO\core\server- Implements
- iRemoteShell
Index
| failOnStderr | ||
| fingerprint | Server identity fingerprint | |
| handle | ||
| host | ||
| jobsFolder | ||
| password | ||
| port | ||
| privateKey | ||
| publicKey | ||
| useAgent | ||
| user | ||
| checkFingerprint() | Verify the server's host key against $fingerprint, and refuse to go any further if it doesn't match -- or if it cannot be checked at all. | |
| connect() | Open the SSH session and authenticate, per $useAgent/$publicKey/$password/$fingerprint. | |
| debugName() | ||
| disconnect() | ||
| exec($cmd, $cwd, $exitCode) | Run a command remotely and wait for it to finish. | |
| execBackground($cmd, $cwd, $env, $id) | Run a command remotely *without waiting for it*, detached from this session: it survives the end of the command, the end of the SSH connection and the end of this PHP process, and its output, exit … | |
| execSafe($cmd, $error, $cwd, $exitCode) | Run a command remotely and wait for it to finish, never throwing on a non-zero exit code or stderr output (exec for that). | |
| expectedFingerprint() | ||
| getJob($id) | Bind back to a job started earlier (by anybody, including another process), by id. | |
| getJobs() | Every job still present under $jobsFolder on the remote host, finished ones included (a job's folder stays until somebody calls SSHJob::clean). | |
| isConnected() | ||
| lastError() | ||
| onDebug($message, $language, $always_display) | ssh2_connect()'s 'debug' callback -- logs every SSH protocol debug message via debug. | |
| onDisconnect($reason, $message, $language) | ssh2_connect()'s 'disconnect' callback -- logs the disconnect and clears $handle so isConnected reports false. | |
| openExec($cmd, $cwd) | Run a command remotely, returning a live stream to its stdout/stderr (SSHStream) -- unlike exec / | |
| openShell($type, $env, $width, $height, $sizeType) | Create a stream to shell | |
| quote($value) static | Quote a value for a *remote POSIX* shell: single quotes, always, with any embedded single quote closed, escaped and reopened -- the only form a POSIX shell interprets nothing inside of. | |
| receive($remoteFile, $localFile) | Receive ssh file | |
| send($localFile, $remoteFile, $mode) | Send file over ssh | |
| __construct($user, $host, $port, $password, $fingerprint, $publicKey, $privateKey) | ||
| __destruct() | Destructor. Disconnects if still connected. |
Properties11
failOnStderr
- Type
- bool
fingerprint
Server identity fingerprint
handle
host
- Type
- string
jobsFolder
- Type
- string
password
port
- Type
- int
privateKey
publicKey
useAgent
- Type
- bool
user
- Type
- string
Methods21
checkFingerprint()
SSH.php:167Verify the server's host key against $fingerprint, and refuse to go any further if it doesn't match -- or if it cannot be checked at all.
| returns | void | |
| throws | if $fingerprint isn't an MD5 or SHA1 hex digest (expectedFingerprint). | |
| throws | if the fingerprint cannot be read, or doesn't match. |
connect()
SSH.php:107Open the SSH session and authenticate, per $useAgent/$publicKey/$password/$fingerprint.
| returns | void | |
| throws | if the connection, the host key verification, or the authentication fails. | |
| throws | if there is no usable way to log in, or the key files are unreadable. |
debugName()
SSH.php:79| returns | string | e.g. 'SSH://user@example.com:22'. |
disconnect()
SSH.php:225| returns | bool | true if a connection was actually closed; false if there wasn't one to close. |
exec($cmd, $cwd, $exitCode)
SSH.php:318Run a command remotely and wait for it to finish.
- See
Shell::exec(exit code), which is what makes the sharediShellcontract mean the same thing locally and remotely.
| $cmd | string | |
| $cwd = null | null | string | the remote working directory to run it from. |
| $exitCode = null | null | int | |
| returns | string | the command's stdout output. |
| throws | if the command failed: a non-zero exit code, or -- only when the remote host reported no exit code at all, or when $failOnStderr is on -- any stderr output. NOTE: this used to key off stderr alone, which both threw on the progress output of every normal git/tar/ curl invocation and let a command that exited 3 silently look like a success. It now matches |
execBackground($cmd, $cwd, $env, $id)
SSH.php:349Run a command remotely *without waiting for it*, detached from this session: it survives the end of the command, the end of the SSH connection and the end of this PHP process, and its output, exit code and liveness can be read back at any time -- from another request or another machine -- through its
- See
SSHJob::$id.,openExecis the other non-blocking option, and the opposite trade-off: a live stream, but the command dies with the channel.
| $cmd | string | the command -- written to a remote file verbatim and run with sh, so no escaping is needed and anything a shell accepts works (pipes, &&, quotes, several lines). |
| $cwd = null | null | string | the remote working directory to run it from. |
| $env = null | null | array | environment variables to export first, as name => value. |
| $id = null | null | string | the job id, defaults to a generated one (SSHJob::NewId). |
| returns | SSHJob | |
| throws | if the remote host didn't confirm the launch. |
execSafe($cmd, $error, $cwd, $exitCode)
SSH.php:297Run a command remotely and wait for it to finish, never throwing on a non-zero exit code or stderr output (exec for that).
expectedFingerprint()
SSH.php:190- See
$fingerprintin the shapessh2_fingerprint()returns one: UPPERCASE hex digits, no separators. Takes it as the tools print it --MD5:ab:cd:...(ssh-keygen -l -E md5),ab:cd:...,ABCD...-- so a leadingMD5:orSHA1:, colons, blanks and case are all ignored. NOTE: the label used to be read as digits too: theD5ofMD5:made an MD5 pasted as printed 34 digits long, and it was refused by a message telling to get it fromssh-keygen -E md5.
| returns | string | 32 hex digits (an MD5) or 40 (a SHA1). |
| throws | if it is anything else -- a base64 fingerprint included: the SHA256:... ssh-keygen -l prints by default (the ssh2 extension computes no SHA256) or the SHA1:... of -E sha1. |
getJob($id)
SSH.php:360Bind back to a job started earlier (by anybody, including another process), by id.
getJobs()
SSH.php:369Every job still present under $jobsFolder on the remote host, finished ones included (a job's folder stays until somebody calls SSHJob::clean).
| returns | array | keyed by id, oldest first (SSHJob::NewId ids sort chronologically). |
isConnected()
SSH.php:220lastError()
SSH.php:210| returns | string | the message of the last PHP warning (the ssh2 functions only report their real reason that way), or 'unknown reason' if there was none. |
onDebug($message, $language, $always_display)
SSH.php:237ssh2_connect()'s 'debug' callback -- logs every SSH protocol debug message via debug.
onDisconnect($reason, $message, $language)
SSH.php:242ssh2_connect()'s 'disconnect' callback -- logs the disconnect and clears $handle so isConnected reports false.
openExec($cmd, $cwd)
SSH.php:276Run a command remotely, returning a live stream to its stdout/stderr (SSHStream) -- unlike exec /
- See
execSafe, doesn't wait for it to finish. WARNING: "doesn't wait" only goes as far as this SSH session: the command runs *inside* the channel, so closing the stream, disconnecting, or just reaching the end of the script kills it. UseexecBackgroundfor something that has to outlive the connection.
| $cmd | string | |
| $cwd = null | null | string | the remote working directory to run it from -- quoted for the remote shell (quote) and prefixed as cd <cwd> && ..., since ssh2_exec takes one command string and has no working-directory argument of its own (unlike Process). |
| returns | SSHStream | |
| throws | if the command couldn't be started. |
openShell($type, $env, $width, $height, $sizeType)
SSH.php:256Create a stream to shell
quote($value)
staticSSH.php:94Quote a value for a *remote POSIX* shell: single quotes, always, with any embedded single quote closed, escaped and reopened -- the only form a POSIX shell interprets nothing inside of.
receive($remoteFile, $localFile)
SSH.php:386Receive ssh file
send($localFile, $remoteFile, $mode)
SSH.php:400Send file over ssh
__construct($user, $host, $port, $password, $fingerprint, $publicKey, $privateKey)
SSH.php:62| $user | string | |
| $host | string | |
| $port = 22 | int | |
| $password = null | null | string | used for password auth (connect), or to decrypt $privateKey if it's encrypted. |
| $fingerprint = null | null | string | if set, connect verifies the server's host key matches this before authenticating -- an MD5 (32 hex characters) or SHA1 (40) digest, the only two the ssh2 extension can compute, in hex. A leading MD5:/SHA1:, colons and case are ignored, so MD5:ab:cd:... as ssh-keygen -l -E md5 prints it works, and so do ab:cd:..., ABCD... and abcd...; base64 does not. |
| $publicKey = null | null | string | path to a public key file -- if set, connect authenticates via key pair instead of $password. |
| $privateKey = null | null | string | path to the matching private key file (required together with $publicKey). |
| throws | if the ssh2 extension isn't installed. |
__destruct()
SSH.php:74Destructor. Disconnects if still connected.