Skip to content

Instantly share code, notes, and snippets.

@kkurahar
Last active December 28, 2015 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkurahar/7508074 to your computer and use it in GitHub Desktop.
Save kkurahar/7508074 to your computer and use it in GitHub Desktop.
ssh connect to EC2(AWS) from PHP(FuelPHP) http://kkurahar.github.io/blog/2013/11/16/php-ssh-ec2/
<?php
return array(
'ssh_key' => 'your_keypair_file', // /path/to/key.pem
'ec2_host' => 'your_ec2_ipaddress', // 54.251.xx.xx
'ec2_user' => 'your_ec2_user', // ec2-user
}
<?php
namespace Fuel\Tasks;
// Include the Composer autoloader
require_once VENDORPATH . "autoload.php";
class Ssh_Connect_Ec2 {
public function run() {
$username = \Config::get('ec2_user');
$host = \Config::get('ec2_host');
$ssh_key = \Config::get('ssh_key');
$key = new \Crypt_RSA();
$key->loadKey(file_get_contents($ssh_key));
$ssh = new \Net_SSH2($host);
if (!$ssh->login($username, $key)) {
exit('Login Failed');
}
echo $ssh->exec("cd /opt/aws && ls -ltr");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment