定制 sandeshjangam/tiny-ssh-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sandeshjangam/tiny-ssh-php

最新稳定版本:v1.1.0

Composer 安装命令:

composer require sandeshjangam/tiny-ssh-php

包简介

Tiny SSH package that allows you to execute commands over SSH connections.

README 文档

README

Tiny SSH package that allows you to execute commands over SSH connections. It supports both password and private key authentication and is built on the phpseclib library.

Installation

Install the package via composer:

composer require sandeshjangam/tiny-ssh-php

Usage

Simple SSH command using password authentication:

$ssh = (new Ssh)
    ->host($ip)
    ->port(22)
    ->user($user)
    ->password($password)
    // ->privateKey($privateKey)
    // ->privateKeyPath($privateKeyPath)
    ->connect();

$response = $ssh->execute('whoami');

$response->getOutput();  // 'username'
$response->getError();   // ''

Response of a command

Get the output:

$response->getOutput(); // It returns the `stdout`

Get the error if any:

$response->getError(); // It returns the `stderr`

Get the exit status:

$response->getExitStatus(); // 0 for success. To check if the command ran successfully

Running multiple commands

To execute multiple commands pass an array of commands:

$response = $ssh->execute(['whoami', 'ls -la']);

Or pass the commands as a string separated by &&:

$response = $ssh->execute('whoami && ls -la');

Use timeout for the long running command

You can set the timeout. Default is 10 seconds:

->timeout(60) // 60 seconds

Use private key as a string or file

You can use Private Key content:

->privateKey('private_key_content')

Or Private Key file path:

->privateKeyPath('/home/user/.ssh/id_rsa')

Upload or Download files and directories

To upload or download files and directories, you'll need to use the SFTP class:

$sftp = (new Sftp)
    ->host($ip)
    ->port(22)
    ->user($user)
    ->password($password)
    // ->privateKey($privateKey)
    // ->privateKeyPath($privateKeyPath)
    ->connect();

To upload a file or directory to the remote server:

$sftp->upload('local/file/path', 'remote/file/path')

To download a file or directory from the remote server:

$sftp->download('remote/file/path', 'local/file/path')

Disconnect SSH or SFTP connection

To disconnect the SSH connection:

$ssh->disconnect();

To disconnect the SFTP connection:

$sftp->disconnect();

Testing

composer test

Credits

License

The MIT License (MIT).

统计信息

  • 总下载量: 6.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-12