消息:类“PublicKeyLoader”未找到(phpseclib)

发布于 2025-01-13 07:25:06 字数 564 浏览 0 评论 0原文

我正在尝试使用 php (phpseclib) 使用私钥将 SSH 连接到 EC2 服务器。我已经从 GitHub 下载了 phpseclib 并将其添加到库文件夹中。

我的代码:

/* $dir --- contains my library folder path */
include($dir.'phpseclib3/Net/SSH2.php');
include($dir.'phpseclib3/Crypt/PublicKeyLoader.php');
$key = new PublicKeyLoader();
$key->loadPrivateKey(file_get_contents($ppkpath));

$ssh = new SSH2('ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com');
if (!$ssh->login('ec2-user', $key)) {
    exit('Login Failed');
}

执行此代码时出现以下错误

未找到“PublicKeyLoader”类

I am trying to connect SSH to EC2 server with privatekey using php (phpseclib). I've downloaded phpseclib from GitHub and added into libraries folder.

My code:

/* $dir --- contains my library folder path */
include($dir.'phpseclib3/Net/SSH2.php');
include($dir.'phpseclib3/Crypt/PublicKeyLoader.php');
$key = new PublicKeyLoader();
$key->loadPrivateKey(file_get_contents($ppkpath));

$ssh = new SSH2('ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com');
if (!$ssh->login('ec2-user', $key)) {
    exit('Login Failed');
}

while executing this I got the following error

Class 'PublicKeyLoader' not found

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雨成花 2025-01-20 07:25:06

您不加载名称空间。

$key = new phpseclib3\Crypt\PublicKeyLoader();

如果您与 Composer 一起安装它,那么最好包含 Composer 自动加载器。

include_once('vendor/autoload.php');

然后使用 use 语句将类加载到当前名称空间中。

use phpseclib3\Crypt\PublicKeyLoader;

这样你的代码就会变成:

include_once('vendor/autoload.php');

use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Net\SSH2;

$key = new PublicKeyLoader();
$key->loadPrivateKey(file_get_contents($ppkpath));

$ssh = new SSH2('ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com');
if (!$ssh->login('ec2-user', $key)) {
    exit('Login Failed');
}

You don't load the namespace.

$key = new phpseclib3\Crypt\PublicKeyLoader();

Better would it be if you installed it with composer is that you include the composer autoloader.

include_once('vendor/autoload.php');

And then load the class in your current namespace with the use statement.

use phpseclib3\Crypt\PublicKeyLoader;

That way your code would become:

include_once('vendor/autoload.php');

use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Net\SSH2;

$key = new PublicKeyLoader();
$key->loadPrivateKey(file_get_contents($ppkpath));

$ssh = new SSH2('ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com');
if (!$ssh->login('ec2-user', $key)) {
    exit('Login Failed');
}
冧九 2025-01-20 07:25:06

我按照@Tschallacka 所说尝试过。效果很好。但我稍微改变了代码如下。

include_once('vendor/autoload.php');

use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Net\SSH2;

$key = PublicKeyLoader::load(file_get_contents($ppkpath));

$ssh = new SSH2('ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com');
if (!$ssh->login('ec2-user', $key)) {
    exit('Login Failed');
}

I tried as @Tschallacka said. It works fine. But slightly I rechange the code as follows.

include_once('vendor/autoload.php');

use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Net\SSH2;

$key = PublicKeyLoader::load(file_get_contents($ppkpath));

$ssh = new SSH2('ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com');
if (!$ssh->login('ec2-user', $key)) {
    exit('Login Failed');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文