mikemeier/php-ssh

为php ssh2扩展提供一个面向对象的包装器。

1.0.3 2014-06-13 12:12 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:27:58 UTC


README

这是一个从https://github.com/Herzult/php-ssh的分支,因为没有提供真正的版本标签。

PHP SSH

构建状态 (master) 构建状态 (development)

为php ssh2扩展提供一个面向对象的包装器。

要求

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

安装

将php-ssh库文件下载到您的项目中。如果您的项目使用git进行版本控制,最好将其添加为子模块

$ git submodule add https://github.com/Herzult/php-ssh.git vendor/php-ssh

然后,您必须将其添加到您的自动加载器中。如果您没有自动加载器,可以使用库中提供的自动加载器。以下是如何使用它的示例

<?php

require_once __DIR__.'/vendor/php-ssh/src/ClassLoader/UniversalClassLoader.php';

use ClassLoader\UniversalClassLoader;

$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
    'Ssh' => __DIR__.'/vendor/php-ssh/src'
));

$loader->register();

安装(通过composer)

下载composer。使用composer安装php-ssh

$ php composer.phar install

用法

连接配置

要建立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());

这将从您的配置文件中的主机和身份声明中获取您的公共和私人密钥。

子系统

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

Sftp

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

<?php

// the session creation

$sftp = $session->getSftp();

有关可用方法,请参阅Ssh\Sftp类。

Publickey

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

<?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类。