{"id":35407134,"url":"https://github.com/cesargb/php-ssh2-client","last_synced_at":"2026-01-02T13:01:56.269Z","repository":{"id":318186476,"uuid":"1070273715","full_name":"cesargb/php-ssh2-client","owner":"cesargb","description":"A simple PHP library for establishing SSH connections using the ext-ssh2 extension.","archived":false,"fork":false,"pushed_at":"2025-10-22T19:21:23.000Z","size":51,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-22T19:25:01.178Z","etag":null,"topics":["php","ssh-client"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cesargb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-10-05T16:04:06.000Z","updated_at":"2025-10-22T19:21:27.000Z","dependencies_parsed_at":"2025-10-05T22:07:19.894Z","dependency_job_id":"0726a6de-57fd-47e9-8d6d-c161a623c061","html_url":"https://github.com/cesargb/php-ssh2-client","commit_stats":null,"previous_names":["cesargb/php-ssh","cesargb/php-ssh2-client"],"tags_count":0,"template":false,"template_full_name":"descom-es/php-skeleton","purl":"pkg:github/cesargb/php-ssh2-client","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cesargb%2Fphp-ssh2-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cesargb%2Fphp-ssh2-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cesargb%2Fphp-ssh2-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cesargb%2Fphp-ssh2-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cesargb","download_url":"https://codeload.github.com/cesargb/php-ssh2-client/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cesargb%2Fphp-ssh2-client/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28173298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2026-01-02T02:00:06.235Z","response_time":54,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","ssh-client"],"created_at":"2026-01-02T13:00:52.063Z","updated_at":"2026-01-02T13:01:56.246Z","avatar_url":"https://github.com/cesargb.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP SSH2 Client\n\nA PHP SSH2 client wrapper providing a clean and modern interface to the ssh2 extension.\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Basic Usage](#basic-usage)\n- [Authentication Methods](#authentication-methods)\n  - [Password Authentication](#password-authentication)\n  - [Public Key Authentication](#public-key-authentication-with-passphrase)\n  - [Agent-Based Authentication](#agent-based-authentication)\n- [Executing Commands](#executing-commands)\n  - [Basic Command Execution](#basic-command-execution)\n  - [Exception Handling with throw()](#exception-handling-with-throw)\n- [SCP File Transfers](#scp-file-transfers)\n  - [Uploading Files](#uploading-a-file)\n  - [Downloading Files](#downloading-a-file)\n- [Testing](#testing)\n\n## Installation\n\nInstall the package via Composer:\n\n``` bash\ncomposer require cesargb/ssh2-client\n```\n\n**Requirements:**\n- PHP 8.2 or higher\n- PHP SSH2 extension (`ext-ssh2`)\n\n## Basic Usage\n\n``` php\nrequire 'vendor/autoload.php';\n\nuse Cesargb\\Ssh\\Ssh2Client;\n\n// Connect to the SSH server\n$sshClient = Ssh2Client::connect(host: 'localhost');\n\n// Get server fingerprint\n$fingerprint = $sshClient-\u003efingerPrint();\necho \"Server Fingerprint: {$fingerprint}\\n\";\n\n// Authenticate\n$sshSession = $sshClient-\u003ewithAuthPassword('username', 'password');\n\n// Execute a command\n$commandResult = $sshSession-\u003ecommand()-\u003eexecute('ls -la');\n\n// Check the result\nif (! $commandResult-\u003esucceeded()) {\n    echo \"Error Output: {$commandResult-\u003eerrorOutput}\\n\";\n    exit($commandResult-\u003egetExitStatus());\n}\n\necho \"Command Output: {$commandResult-\u003eoutput}\\n\";\n\n// Disconnect when done\n$sshSession-\u003edisconnect();\n```\n\n## Authentication Methods\n\nThe library supports multiple authentication methods. All authentication methods return an `SshSession` object that you can use to execute commands or transfer files.\n\n### Password Authentication\n\nAuthenticate using a username and password:\n\n``` php\n$sshClient = Ssh2Client::connect(host: 'example.com', port: 22);\n$sshSession = $sshClient-\u003ewithAuthPassword(\n    username: 'root',\n    password: 'root_password'\n);\n```\n\n### Public Key Authentication with Passphrase\n\nAuthenticate using SSH key pairs:\n\n``` php\n$sshClient = Ssh2Client::connect(host: 'example.com', port: 22);\n$sshSession = $sshClient-\u003ewithAuthPublicKey(\n    username: 'root',\n    publicKey: '/path/to/public/key.pub',\n    privateKey: '/path/to/private/key',\n    passphrase: 'passphrase if required'  // Optional, use empty string if no passphrase\n);\n```\n\n### Agent-Based Authentication\n\nAuthenticate using the SSH agent:\n\n``` php\n$sshClient = Ssh2Client::connect(host: 'example.com', port: 22);\n$sshSession = $sshClient-\u003ewithAuthAgent('username');\n```\n\n## Executing Commands\n\n### Basic Command Execution\n\nExecute commands and check the results manually:\n\n``` php\n// Connect and authenticate\n$sshSession = Ssh2Client::connect(host: 'example.com', port: 22)\n    -\u003ewithAuthPassword('username', 'password');\n\n// Execute a command\n$result = $sshSession-\u003ecommand()-\u003eexecute('ls -l');\n\n// Check if the command succeeded\nif ($result-\u003esucceeded()) {\n    echo \"Success: {$result-\u003eoutput}\\n\";\n} else {\n    echo \"Failed: {$result-\u003eerrorOutput}\\n\";\n    echo \"Exit code: {$result-\u003egetExitStatus()}\\n\";\n}\n\n// Disconnect when done\n$sshSession-\u003edisconnect();\n```\n\n**Result properties:**\n- `$result-\u003esucceeded()` - Returns `true` if the command executed successfully (exit status 0)\n- `$result-\u003egetExitStatus()` - Returns the exit status code of the command (e.g., 0 for success, 127 for command not found)\n- `$result-\u003eoutput` - Contains the command output from stdout\n- `$result-\u003eerrorOutput` - Contains any error output from stderr\n- `$result-\u003ecommand` - The command that was executed\n\n### Exception Handling with throw()\n\nFor cleaner error handling, you can use the `throw()` method to automatically throw exceptions when commands fail:\n\n``` php\nuse Cesargb\\Ssh\\Exceptions\\SshCommandException;\n\ntry {\n    // Enable automatic exception throwing\n    $sshSession = Ssh2Client::connect(host: 'example.com', port: 22)\n        -\u003ewithAuthPassword('username', 'password')\n        -\u003ethrow();  // Enable exception mode\n    \n    // Execute commands - will throw exception on failure\n    $result = $sshSession-\u003ecommand()-\u003eexecute('ls -l');\n    echo \"Success: {$result-\u003eoutput}\\n\";\n    \n    // This command will throw an exception if it fails\n    $result = $sshSession-\u003ecommand()-\u003eexecute('some-command');\n    \n} catch (SshCommandException $e) {\n    echo \"Command failed: {$e-\u003egetMessage()}\\n\";\n    echo \"Exit code: {$e-\u003eresult-\u003egetExitStatus()}\\n\";\n    echo \"Error output: {$e-\u003eresult-\u003eerrorOutput}\\n\";\n} finally {\n    $sshSession-\u003edisconnect();\n}\n```\n\n**Benefits of using `throw()`:**\n- Eliminates the need for manual success checks after each command\n- Provides cleaner error handling with try-catch blocks\n- The exception contains the full `ExecResult` object with all command details\n- Ideal for scripts where command failures should stop execution\n\n**Note:** When using `throw()`, any command that returns a non-zero exit status will throw a `SshCommandException`.\n\n## SCP File Transfers\n\nTransfer files securely between local and remote systems using SCP (Secure Copy Protocol).\n\n### Uploading a File\n\nUpload a local file to the remote server:\n\n``` php\n$sshSession = Ssh2Client::connect(host: 'example.com', port: 22)\n    -\u003ewithAuthPassword('username', 'password');\n\n$scpResult = $sshSession\n    -\u003escp()\n    -\u003eupload('/local/path/to/file.txt')\n    -\u003eto('/remote/path/to/file.txt');\n\nif ($scpResult-\u003esucceeded()) {\n    echo \"File uploaded successfully.\\n\";\n} else {\n    echo \"File upload failed.\\n\";\n}\n\n$sshSession-\u003edisconnect();\n```\n\n### Downloading a File\n\nDownload a file from the remote server to your local system:\n\n``` php\n$sshSession = Ssh2Client::connect(host: 'example.com', port: 22)\n    -\u003ewithAuthPassword('username', 'password');\n\n$scpResult = $sshSession\n    -\u003escp()\n    -\u003edownload('/remote/path/to/file.txt')\n    -\u003eto('/local/path/to/file.txt');\n\nif ($scpResult-\u003esucceeded()) {\n    echo \"File downloaded successfully.\\n\";\n} else {\n    echo \"File download failed.\\n\";\n}\n\n$sshSession-\u003edisconnect();\n```\n\n**Note:** SCP operations also support the `throw()` method for automatic exception handling, similar to command execution.\n\n## Testing\n\n``` bash\ncomposer test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcesargb%2Fphp-ssh2-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcesargb%2Fphp-ssh2-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcesargb%2Fphp-ssh2-client/lists"}