百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

ssh

> 编程语言
开源

一个用于通过 SSH 连接执行命令的轻量级包

831 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个用于通过 SSH 连接执行命令的轻量级包

A lightweight package to execute commands over an SSH connection

You can execute an SSH command like this:

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

It will return an instance of Symfony's Process.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

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. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

bash
composer require spatie/ssh

Usage

You can execute an SSH command like this:

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

It will return an instance of Symfony's Process.

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

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

Getting the result of a command

To check if your command ran ok

php
$process->isSuccessful();

This is how you can get the output

php
$process->getOutput();

Running multiple commands

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

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

Choosing a port

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

php
$port = 123;

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

Alternatively you can use the usePort function:

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

Using a password

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

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

Alternatively you can use the usePassword function:

php
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

You can set a timeout for the command.

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

Specifying a jump host

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

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

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:

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

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

Specifying the private key to use

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

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

Disable Strict host key checking

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

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

Enable quiet mode

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

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

Disable Password Authentication

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

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

Uploading & downloading files and directories

You can upload files & directories to a host using:

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

Or download them:

php
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

Behind the scenes all commands will be performed using Symfonys Process.

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

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

Immediately responding to output

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

php
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

If your target is a Windows machine, use the onWindows method.

php
Ssh::create('user', 'host')->onWindows()->execute('dir');

By default this package pipes your commands to bash on the remote host. On Windows the remote shell is cmd.exe, which handles input differently and always reports a successful exit code, even when a command fails. The onWindows method passes your commands as an argument to ssh instead, so cmd.exe runs them through cmd.exe /c and their real exit code (and thus isSuccessful()) is preserved.

Testing

bash
composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

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

Alternatives

If you need some more features, take a look at DivineOmega/php-ssh-connection.

Credits

  • Freek Van der Herten
  • All Contributors

The Ssh class contains code taken from laravel/envoy

License

The MIT License (MIT). Please see License File for more information.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPphpssh

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言