{"id":13549005,"url":"https://github.com/spatie/ssh","last_synced_at":"2025-05-15T01:04:32.047Z","repository":{"id":37515603,"uuid":"237949290","full_name":"spatie/ssh","owner":"spatie","description":"A lightweight package to execute commands over an SSH connection","archived":false,"fork":false,"pushed_at":"2024-12-23T10:28:15.000Z","size":163,"stargazers_count":803,"open_issues_count":0,"forks_count":83,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-04-14T02:58:43.752Z","etag":null,"topics":["php","ssh"],"latest_commit_sha":null,"homepage":"https://spatie.be/open-source","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","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},"funding":{"custom":"https://spatie.be/open-source/support-us"}},"created_at":"2020-02-03T11:25:59.000Z","updated_at":"2025-04-09T13:46:01.000Z","dependencies_parsed_at":"2023-12-18T15:49:15.539Z","dependency_job_id":"a0613a6d-4d93-4c37-8ed7-b7b0385bce73","html_url":"https://github.com/spatie/ssh","commit_stats":{"total_commits":149,"total_committers":44,"mean_commits":"3.3863636363636362","dds":0.6308724832214765,"last_synced_commit":"ad9005e6293b511306a62ca3bda08f8ec89118fb"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fssh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fssh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fssh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fssh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/ssh/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248813789,"owners_count":21165633,"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","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"],"created_at":"2024-08-01T12:01:17.165Z","updated_at":"2025-04-14T02:58:48.874Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://spatie.be/open-source/support-us"],"categories":["PHP"],"sub_categories":[],"readme":"# A lightweight package to execute commands over an SSH connection\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/ssh.svg?style=flat-square)](https://packagist.org/packages/spatie/ssh)\n[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/spatie/ssh/run-tests?label=tests)](https://github.com/spatie/ssh/actions?query=workflow%3Arun-tests+branch%3Amaster)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/ssh.svg?style=flat-square)](https://packagist.org/packages/spatie/ssh)\n\nYou can execute an SSH command like this:\n\n```php\nSsh::create('user', 'host')-\u003eexecute('your favorite command');\n```\n\nIt will return an instance of [Symfony's `Process`](https://symfony.com/doc/current/components/process.html).\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/ssh.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/ssh)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/ssh\n```\n\n## Usage\n\nYou can execute an SSH command like this:\n\n```php\n$process = Ssh::create('user', 'example.com')-\u003eexecute('your favorite command');\n```\n\nIt will return an instance of [Symfony's `Process`](https://symfony.com/doc/current/components/process.html).\n\nIf you don't want to wait until the execute commands complete, you can call `executeAsync`\n\n```php\n$process = Ssh::create('user', 'example.com')-\u003eexecuteAsync('your favorite command');\n```\n\n### Getting the result of a command\n\nTo check if your command ran ok\n\n```php\n$process-\u003eisSuccessful();\n```\n\n\nThis is how you can get the output\n\n```php\n$process-\u003egetOutput();\n```\n\n### Running multiple commands\n\nTo run multiple commands pass an array to the execute method.\n\n```php\n$process = Ssh::create('user', 'example.com')-\u003eexecute([\n   'first command',\n   'second command',\n]);\n```\n\n### Choosing a port\n\nYou can choose a port by passing it to the constructor.\n\n\n```php\n$port = 123;\n\nSsh::create('user', 'host', $port);\n```\n\nAlternatively you can use the `usePort` function:\n\n```php\nSsh::create('user', 'host')-\u003eusePort($port);\n```\n\n### Using a password\n\nYou can use the constructor to specify a password to use.\n\n```php\nSsh::create('user', 'host', port, 'password');\n```\n\nAlternatively you can use the `usePassword` function:\n\n```php\nSsh::create('user', 'host')-\u003eusePassword('password');\n```\n\n### Setting a timeout\n\nYou can set a timeout for the command.\n\n\n```php\nSsh::create('user', 'host')-\u003esetTimeout(100);\n```\n\n### Specifying a jump host\n\nIf using a jump/proxy/bastion host, the `useJumpHost` function allows you to set the jump hosts details:\n\n```php\nSsh::create('user', 'host')-\u003euseJumpHost(\"$jumpuser@$jumphost:$jumpport\");\n```\n\n### Using SSH multiplexing\n\nIf making many connections to the same host, SSH multiplexing enables re-using one TCP connection. Call `useMultiplexing` function to set control master options:\n\n```php\nSsh::create('user', 'host')-\u003euseMultiplexing($controlPath, $controlPersist);\n\n// Ssh::create('user', 'host')-\u003euseMultiplexing('/home/.ssh/control_masters/%C', '15m');\n\n```\n\n\n### Specifying the private key to use\n\nYou can use `usePrivateKey` to specify a path to a private SSH key to use.\n\n```php\nSsh::create('user', 'host')-\u003eusePrivateKey('/home/user/.ssh/id_rsa');\n```\n\n### Disable Strict host key checking\n\nBy default, strict host key checking is enabled. You can disable strict host key checking using `disableStrictHostKeyChecking`.\n\n```php\nSsh::create('user', 'host')-\u003edisableStrictHostKeyChecking();\n```\n### Enable quiet mode\n\nBy default, the quiet mode is disabled. You can enable quiet mode using `enableQuietMode`.\n\n```php\nSsh::create('user', 'host')-\u003eenableQuietMode();\n```\n\n### Disable Password Authentication\n\nBy default, the password authentication is enabled. You can disable password authentication using `disablePasswordAuthentication`.\n\n```php\nSsh::create('user', 'host')-\u003edisablePasswordAuthentication();\n```\n\n### Uploading \u0026 downloading files and directories\n\nYou can upload files \u0026 directories to a host using:\n\n```php\nSsh::create('user', 'host')-\u003eupload('path/to/local/file', 'path/to/host/file');\n```\n\nOr download them:\n\n```php\nSsh::create('user', 'host')-\u003edownload('path/to/host/file', 'path/to/local/file');\n```\n\nUnder the hood the process will use `scp`.\n\n### Modifying the Symfony process\n\nBehind the scenes all commands will be performed using [Symfonys `Process`](https://symfony.com/doc/current/components/process.html).\n\nYou can configure to the `Process` by using the `configureProcess` method. Here's an example where we disable the timeout.\n\n```php\nSsh::create('user', 'host')-\u003econfigureProcess(fn (Process $process) =\u003e $process-\u003esetTimeout(null));\n```\n\n### Immediately responding to output\n\nYou can get notified whenever your command produces output by passing a closure to `onOutput`. \n\n```php\nSsh::create('user', 'host')-\u003eonOutput(function($type, $line) {echo $line;})-\u003eexecute('whoami');\n```\n\nWhenever there is output that closure will get called with two parameters:\n- `type`: this can be `Symfony\\Component\\Process\\Process::OUT` for regular output and `Symfony\\Component\\Process\\Process::ERR` for error output\n- `line`: the output itself\n\n### Windows Target \n\nIf your target is a Windows machine, you can use the `removeBash` method to remove the bash command from the command line.\n\n```php\nSsh::create('user', 'host')-\u003eremoveBash();\n```\n\n## Testing\n\n``` bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security\n\nIf you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker.\n\n## Alternatives\n\n  If you need some more features, take a look at [DivineOmega/php-ssh-connection](https://github.com/DivineOmega/php-ssh-connection).\n\n## Credits\n\n- [Freek Van der Herten](https://github.com/freekmurze)\n- [All Contributors](../../contributors)\n\nThe `Ssh` class contains code taken from [laravel/envoy](https://laravel.com/docs/6.x/envoy)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fssh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Fssh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fssh/lists"}