消息:类“PublicKeyLoader”未找到(phpseclib)
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不加载名称空间。
如果您与 Composer 一起安装它,那么最好包含 Composer 自动加载器。
然后使用 use 语句将类加载到当前名称空间中。
这样你的代码就会变成:
You don't load the namespace.
Better would it be if you installed it with composer is that you include the composer autoloader.
And then load the class in your current namespace with the use statement.
That way your code would become:
我按照@Tschallacka 所说尝试过。效果很好。但我稍微改变了代码如下。
I tried as @Tschallacka said. It works fine. But slightly I rechange the code as follows.