faudin/php-ssh

提供了一个针对php ssh2扩展的对象封装。(基于herzult包)

v1.1.2 2017-11-29 12:21 UTC

This package is not auto-updated.

Last update: 2024-09-20 02:11:49 UTC


README

构建状态(master)

提供了一个针对php ssh2扩展的对象封装。

需求

您需要PHP版本5.3+并安装了SSH2扩展

安装

将库添加到项目的最佳方式是使用composer

$ composer require faudin/php-ssh:~1.0

使用方法

连接配置

要建立SSH连接,您必须首先定义其配置。为此,创建一个带有所有必需参数的配置实例。

<?php

// simple configuration to connect "my-host"
$configuration = new Ssh\Configuration('my-host');

可用的配置类有

  • Configuration
  • SshConfigFileConfiguration

可以从ssh配置文件(如~/.ssh/config)中获取连接配置和公钥/私钥认证。

<?php

// simple configuration to connect "my-host"
$configuration = new Ssh\SshConfigFileConfiguration('/Users/username/.ssh/config', 'my-host');
$authentication = $configuration->getAuthentication('optional_passphrase', 'optional_username');

创建会话

会话是库提供的SSH功能的主要访问点。

<?php

// ... the configuration creation

$session = new Ssh\Session($configuration);

认证

认证类允许您在SSH会话中进行认证。当您为会话定义认证时,它将在连接时进行认证。

<?php

$configuration = new Ssh\Configuration('myhost');
$authentication = new Ssh\Authentication\Password('John', 's3cr3t');

$session = new Session($configuration, $authentication);

可用的认证有

  • None 基于用户名认证
  • Password 基于密码认证
  • PublicKeyFile 使用公钥进行认证
  • HostBasedFile 使用公钥主机密钥进行认证
  • Agent 使用ssh-agent进行认证

从SshConfigFileConfiguration进行认证

如果您使用ssh配置文件,可以按照以下方式加载您的认证和配置:

<?php

$configuration = new Ssh\SshConfigFileConfiguration('~/.ssh/config', 'my-host');

$session = new Session($configuration, $configuration->getAuthentication());

这将从您的配置文件中的Host和Identity声明中获取您的公钥和私钥。

子系统

一旦您通过SSH会话进行了认证,您就可以使用子系统。

Sftp

您可以使用getSftp()方法轻松访问会话的sftp子系统。

<?php

// the session creation

$sftp = $session->getSftp();

有关可用方法的详细信息,请参阅Ssh\Sftp类。

Scp

您可以使用getScp()方法轻松访问会话的SCP子系统。

<?php

// the session creation

$scp = $session->getScp();
$scp->send('local/path/to/file','remote/path/where/send/file');
$scp->receive('remote/path/to/file','local/path/where/save/file');

有关可用方法的详细信息,请参阅Ssh\Scp类。

Publickey

会话还提供了getPublickey()方法来访问公钥子系统。

<?php

// ... the session creation

$publickey = $session->getPublickey();

有关可用方法的详细信息,请参阅Ssh\Publickey类。

Exec

会话提供了getExec()方法来访问exec子系统。

<?php

// ... the session creation

$exec = $session->getExec();

echo $exec->run('ls -lah');

有关详细信息,请参阅Ssh\Exec类。