简体   繁体   English

通过PHP连接到Amazon EC2实例的SSH连接

[英]SSH Connection to Amazon EC2 Instance Via PHP

Trying to connect to an Amazon EC2 instance using a .pem file, PHP, and phpseclib. 尝试使用.pem文件,PHP和phpseclib连接到Amazon EC2实例。

I have tried what's mentioned in this post: ssh access to ec2 from php 我已经尝试过这篇文章中提到的内容: 从php访问ec2的ssh

However, I keep getting "Error 111. Connection refused in..." 但是,我一直收到“错误111.连接被拒绝......”

When I connect from my own machine using ssh and the same .pem file, there are no errors. 当我使用ssh和相同的.pem文件从我自己的机器连接时,没有错误。

Here is the code from the original post that I am using: 这是我正在使用的原始帖子中的代码:

include('Net/SSH2.php');
include('Crypt/RSA.php');

$key = new Crypt_RSA();

$key->loadKey(file_get_contents('/pathtokey.pem'));


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

I just tested the sample code provided and it works with my Amazon EC2 Ubuntu instance. 我刚刚测试了提供的示例代码,它适用于我的Amazon EC2 Ubuntu实例。

You have to check the obvious as proposed: 你必须检查明显的建议:

  • Hostname 主机名
  • Username 用户名
  • PEM file path PEM文件路径

Are they correct? 他们是对的吗? Are you sure you are using the same ones that you use when connecting from your own machine? 您确定使用的是从自己的机器连接时使用的相同设备吗?

If they are the same, are you testing the code from the same machine? 如果它们相同,您是否在同一台机器上测试代码? If not, does the host from where you are running the code has the port 22 open for outbound connections? 如果没有,运行代码的主机是否为端口连接打开了端口22?

Obvious thing to check, is the port open and allowed for your user? 要检查的明显事项是,端口是否打开并允许您的用户使用?

Try just doing: 试着做:

wget ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com:22

From the machine that's giving you trouble and see if you can open the socket. 从给你麻烦的机器,看看你是否可以打开插座。

I guess you may need to run that command through PHP with shell_exec() to make sure it's being called by the same user/security settings. 我猜你可能需要通过PHP使用shell_exec()来运行该命令,以确保它被相同的用户/安全设置调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM