PHPackages                             spatie/ssh - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. spatie/ssh

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

spatie/ssh
==========

A lightweight package to execute commands over an SSH connection

1.13.1(5mo ago)8322.8M—3.9%8420MITPHPPHP ^7.4|^8.0CI passing

Since Feb 3Pushed 5mo ago9 watchersCompare

[ Source](https://github.com/spatie/ssh)[ Packagist](https://packagist.org/packages/spatie/ssh)[ Docs](https://github.com/spatie/ssh)[ Fund](https://spatie.be/open-source/support-us)[ RSS](/packages/spatie-ssh/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (29)Used By (20)

A lightweight package to execute commands over an SSH connection
================================================================

[](#a-lightweight-package-to-execute-commands-over-an-ssh-connection)

[![Latest Version on Packagist](https://camo.githubusercontent.com/8a8a60d190c1fad4ac2312b567e5d1908829ca8926f32a6e70f9f33106a1b9ea/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f7373682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/ssh)[![GitHub Tests Action Status](https://camo.githubusercontent.com/8b1fc983ffd1f6f030e6771f5c3ebf6300c2ad9b55b8900e1762b146f3b2498b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7370617469652f7373682f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/spatie/ssh/actions?query=workflow%3Arun-tests+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/684e031e39f68ec8010671284f36791c6bb211a865b51e53d3b5a5f137bcfca9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f7373682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/ssh)

You can execute an SSH command like this:

```
Ssh::create('user', 'host')->execute('your favorite command');
```

It will return an instance of [Symfony's `Process`](https://symfony.com/doc/current/components/process.html).

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/fd5a0d0c274733f8bb13285d303f965eab3223d982c521b7aebaeabbd70a7238/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f7373682e6a70673f743d31)](https://spatie.be/github-ad-click/ssh)

We 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).

We 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).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/ssh
```

Usage
-----

[](#usage)

You can execute an SSH command like this:

```
$process = Ssh::create('user', 'example.com')->execute('your favorite command');
```

It will return an instance of [Symfony's `Process`](https://symfony.com/doc/current/components/process.html).

If you don't want to wait until the execute commands complete, you can call `executeAsync`

```
$process = Ssh::create('user', 'example.com')->executeAsync('your favorite command');
```

### Getting the result of a command

[](#getting-the-result-of-a-command)

To check if your command ran ok

```
$process->isSuccessful();
```

This is how you can get the output

```
$process->getOutput();
```

### Running multiple commands

[](#running-multiple-commands)

To run multiple commands pass an array to the execute method.

```
$process = Ssh::create('user', 'example.com')->execute([
   'first command',
   'second command',
]);
```

### Choosing a port

[](#choosing-a-port)

You can choose a port by passing it to the constructor.

```
$port = 123;

Ssh::create('user', 'host', $port);
```

Alternatively you can use the `usePort` function:

```
Ssh::create('user', 'host')->usePort($port);
```

### Using a password

[](#using-a-password)

You can use the constructor to specify a password to use.

```
Ssh::create('user', 'host', port, 'password');
```

Alternatively you can use the `usePassword` function:

```
Ssh::create('user', 'host')->usePassword('password');
```

Please make sure to have the `sshpass` package installed on the local system, otherwise it will silently fail.

### Setting a timeout

[](#setting-a-timeout)

You can set a timeout for the command.

```
Ssh::create('user', 'host')->setTimeout(100);
```

### Specifying a jump host

[](#specifying-a-jump-host)

If using a jump/proxy/bastion host, the `useJumpHost` function allows you to set the jump hosts details:

```
Ssh::create('user', 'host')->useJumpHost("$jumpuser@$jumphost:$jumpport");
```

### Using SSH multiplexing

[](#using-ssh-multiplexing)

If making many connections to the same host, SSH multiplexing enables re-using one TCP connection. Call `useMultiplexing` function to set control master options:

```
Ssh::create('user', 'host')->useMultiplexing($controlPath, $controlPersist);

// Ssh::create('user', 'host')->useMultiplexing('/home/.ssh/control_masters/%C', '15m');
```

### Specifying the private key to use

[](#specifying-the-private-key-to-use)

You can use `usePrivateKey` to specify a path to a private SSH key to use.

```
Ssh::create('user', 'host')->usePrivateKey('/home/user/.ssh/id_rsa');
```

### Disable Strict host key checking

[](#disable-strict-host-key-checking)

By default, strict host key checking is enabled. You can disable strict host key checking using `disableStrictHostKeyChecking`.

```
Ssh::create('user', 'host')->disableStrictHostKeyChecking();
```

### Enable quiet mode

[](#enable-quiet-mode)

By default, the quiet mode is disabled. You can enable quiet mode using `enableQuietMode`.

```
Ssh::create('user', 'host')->enableQuietMode();
```

### Disable Password Authentication

[](#disable-password-authentication)

By default, the password authentication is enabled. You can disable password authentication using `disablePasswordAuthentication`.

```
Ssh::create('user', 'host')->disablePasswordAuthentication();
```

### Uploading &amp; downloading files and directories

[](#uploading--downloading-files-and-directories)

You can upload files &amp; directories to a host using:

```
Ssh::create('user', 'host')->upload('path/to/local/file', 'path/to/host/file');
```

Or download them:

```
Ssh::create('user', 'host')->download('path/to/host/file', 'path/to/local/file');
```

Under the hood the process will use `scp`.

### Modifying the Symfony process

[](#modifying-the-symfony-process)

Behind the scenes all commands will be performed using [Symfonys `Process`](https://symfony.com/doc/current/components/process.html).

You can configure to the `Process` by using the `configureProcess` method. Here's an example where we disable the timeout.

```
Ssh::create('user', 'host')->configureProcess(fn (Process $process) => $process->setTimeout(null));
```

### Immediately responding to output

[](#immediately-responding-to-output)

You can get notified whenever your command produces output by passing a closure to `onOutput`.

```
Ssh::create('user', 'host')->onOutput(function($type, $line) {echo $line;})->execute('whoami');
```

Whenever there is output that closure will get called with two parameters:

- `type`: this can be `Symfony\Component\Process\Process::OUT` for regular output and `Symfony\Component\Process\Process::ERR` for error output
- `line`: the output itself

### Windows Target

[](#windows-target)

If your target is a Windows machine, you can use the `removeBash` method to remove the bash command from the command line.

```
Ssh::create('user', 'host')->removeBash();
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you've found a bug regarding security please mail  instead of using the issue tracker.

Alternatives
------------

[](#alternatives)

If you need some more features, take a look at [DivineOmega/php-ssh-connection](https://github.com/DivineOmega/php-ssh-connection).

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

The `Ssh` class contains code taken from [laravel/envoy](https://laravel.com/docs/6.x/envoy)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

65

—

FairBetter than 99% of packages

Maintenance70

Regular maintenance activity

Popularity65

Solid adoption and visibility

Community43

Growing community involvement

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 51.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~78 days

Recently: every ~149 days

Total

28

Last Release

176d ago

Major Versions

0.0.1 → 1.0.02020-02-04

1.1.0 → v2.x-dev2020-02-10

PHP version history (3 changes)0.0.1PHP ^7.4

1.4.1PHP ^8.0|^7.4

1.7.2PHP ^7.4|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (105 commits)")[![shaunluedeke](https://avatars.githubusercontent.com/u/77498048?v=4)](https://github.com/shaunluedeke "shaunluedeke (10 commits)")[![alexmanase](https://avatars.githubusercontent.com/u/10696975?v=4)](https://github.com/alexmanase "alexmanase (10 commits)")[![Nielsvanpach](https://avatars.githubusercontent.com/u/10651054?v=4)](https://github.com/Nielsvanpach "Nielsvanpach (8 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (8 commits)")[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (6 commits)")[![yob-yob](https://avatars.githubusercontent.com/u/25021150?v=4)](https://github.com/yob-yob "yob-yob (5 commits)")[![rubenvanassche](https://avatars.githubusercontent.com/u/619804?v=4)](https://github.com/rubenvanassche "rubenvanassche (5 commits)")[![m1guelpf](https://avatars.githubusercontent.com/u/23558090?v=4)](https://github.com/m1guelpf "m1guelpf (5 commits)")[![chapeupreto](https://avatars.githubusercontent.com/u/834048?v=4)](https://github.com/chapeupreto "chapeupreto (4 commits)")[![SamuelNitsche](https://avatars.githubusercontent.com/u/24483576?v=4)](https://github.com/SamuelNitsche "SamuelNitsche (4 commits)")[![mallardduck](https://avatars.githubusercontent.com/u/619938?v=4)](https://github.com/mallardduck "mallardduck (4 commits)")[![zaherg](https://avatars.githubusercontent.com/u/27624?v=4)](https://github.com/zaherg "zaherg (3 commits)")[![MatusBoa](https://avatars.githubusercontent.com/u/8343385?v=4)](https://github.com/MatusBoa "MatusBoa (3 commits)")[![mazedlx](https://avatars.githubusercontent.com/u/9453522?v=4)](https://github.com/mazedlx "mazedlx (3 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![iPwnPancakes](https://avatars.githubusercontent.com/u/13839270?v=4)](https://github.com/iPwnPancakes "iPwnPancakes (2 commits)")[![pkboom](https://avatars.githubusercontent.com/u/13960169?v=4)](https://github.com/pkboom "pkboom (2 commits)")[![ebulku](https://avatars.githubusercontent.com/u/11932725?v=4)](https://github.com/ebulku "ebulku (2 commits)")[![bomas13](https://avatars.githubusercontent.com/u/4661591?v=4)](https://github.com/bomas13 "bomas13 (2 commits)")

---

Tags

phpsshspatiessh

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/spatie-ssh/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-ssh/health.svg)](https://phpackages.com/packages/spatie-ssh)
```

###  Alternatives

[spatie/laravel-package-tools

Tools for creating Laravel packages

935125.5M7.0k](/packages/spatie-laravel-package-tools)[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M627](/packages/spatie-laravel-data)[spatie/typescript-transformer

This is my package typescript-transformer

3706.5M16](/packages/spatie-typescript-transformer)[spatie/macroable

A trait to dynamically add methods to a class

72659.6M64](/packages/spatie-macroable)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k17.1M59](/packages/spatie-regex)[spatie/enum

PHP Enums

84429.1M68](/packages/spatie-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
