1

我一直在寻找关于如何使用它php来建立与远程服务器的连接并执行命令的解决方案。研究使我注意到了两种解决方案;phpseclibssh2。我会尽量不让我的问题变成比较帖子。据我所知,它们都是广泛使用的解决方案。ssh2是一个 php 扩展,phpseclib而是一个纯 php 库。看来您也可以phpseclib与 结合使用ssh2。我更愿意避免需要安装扩展,所以我更倾向于使用phpseclib. 不过我确实有些担心。

ssh2有直接文档,php这是否意味着它是一个更好的解决方案?

我看到的大多数帖子phpseclib都是几年前的,这仍然是一个“现代”的解决方案吗?

phpseclib在这里找到)说它是为php 42.0面向的兼容性而构建的php 5。对于那些与之合作的人来说,这是一个可以接受的解决方案php 7吗?

这是一个使用 anRSA key建立ssh连接的示例phpseclib 2.0

require __DIR__ . '/vendor/autoload.php';

use phpseclib\Net\SSH2;
use phpseclib\Crypt\RSA;

$ssh = new SSH2('www.domain.tld');
$key = new RSA();
$key->loadKey(file_get_contents('privatekey'));
if (!$ssh->login('username', $key)) {
    exit('Login Failed');
}

echo $ssh->exec('pwd');

我仍然很新,ssh但如果我正确理解上述内容,只要远程服务器有一个.pub密钥,我就可以简单地提供我的本地私钥(和用户)的连接,我应该能够连接吗?公钥是否需要在某个特定的地方?

4

1 回答 1

1

phpseclib 1.0, in theory, works on PHP 4.4, but phpseclib 2.0 requires PHP 5.3+. 3.0 requires 5.6+. All versions of phpseclib work all the way through PHP 7.4, as can be seen on the Travis CI test results:

https://travis-ci.org/github/phpseclib/phpseclib

(older versions of PHP aren't unit tested because Travis CI doesn't support them; Docker containers could be used but yeh)

I am still fairly new to ssh but if I understand the above correctly, so long as the remote server has a .pub key I can simply supply my connection with my local private key (and user) and I should be able to connect? Does the public key need to be somewhere specific?

Well with OpenSSH server the public key would need to live in ~/.ssh/authorized_keys. On the client end of things all you really need is the private key. The public key can be extracted from the private key.

于 2020-07-01T16:33:43.200 回答