Last active
December 28, 2015 13:29
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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