SSH Connecting to Amazon EC2 Instance via PHP

Attempting to connect to an Amazon EC2 instance using a .pem file, PHP, and phpseclib.

I tried what was mentioned in this post: ssh access to ec2 from php

However, I keep getting "Error 111. Connection refused ..."

When I connect to my machine using ssh and the same .pem file, there are no errors.

Here is the code from the original post I'm 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');
}
+3
source share
2 answers

I just tested the sample code and it works with my Amazon EC2 Ubuntu instance.

You should check the obvious as suggested:

  • Hostname
  • Username
  • PEM file path

? , , ?

, ? , , , 22, ?

+4

, , ?

:

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

, , , .

, , , PHP shell_exec(), , /.

0

All Articles