Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH Connection to Amazon EC2 Instance Via PHP

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

I have tried what's mentioned in this post: ssh access to ec2 from php

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

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

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');
}
like image 279
user883210 Avatar asked Jul 31 '26 22:07

user883210


1 Answers

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

You have to check the obvious as proposed:

  • Hostname
  • Username
  • PEM file path

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?

like image 200
adosaiguas Avatar answered Aug 03 '26 11:08

adosaiguas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!