如何使用PHP和SSH连接到Amazon EC2 Server

我无法使用PHP通过pem文件连接到Amazon EC2

$pemFile = file_get_contents($this->_pemFileLocation);

$out = "";
$key_private = openssl_pkey_export($pemFile,$out);

我需要连接并获取从ssh2_auth_pubkey_file()返回的连接对象,以便我可以在Amazon EC2实例或任何其他使用pem文件的服务器上运行终端命令。

wxt112 回答:如何使用PHP和SSH连接到Amazon EC2 Server

使用phpseclib http://phpseclib.sourceforge.net/

找到了解决方案
$key = new \phpseclib\Crypt\RSA();

$key->loadKey(file_get_contents("key.pem"));

$ssh = new \phpseclib\Net\SSH2("18.191.164.167");

/* ubuntu is the username used by amazon ec2 */
if (!$ssh->login("ubuntu",$key)) {
   throw new \Exception('Login Failed',E_WARNING);
}else{
    /* run a command and get the results as a string */
    var_dump($ssh->exec("ping davidclews.com"));
}
本文链接:https://www.f2er.com/3164536.html

大家都在问