gino-pane / php-ssh
为PHP SSH2扩展提供了一个面向对象的包装器。
v1.2.0
2020-02-19 13:46 UTC
Requires
- php: >=5.3.2
- ext-ssh2: *
Requires (Dev)
- phpunit/phpunit: 3.7.*
- symfony/filesystem: ~2.4
This package is auto-updated.
Last update: 2024-09-20 00:12:38 UTC
README
为php ssh2扩展提供了一个面向对象的包装器。
要求
您需要PHP版本5.3+并安装SSH2扩展。
安装
将库添加到项目的最佳方式是使用composer。
$ composer require herzult/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());
这将从您的配置文件中的主机和身份声明中获取您的公钥和私钥。
子系统
一旦通过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
类。