繁体   English   中英

如何使用 PHP 和 SSH 连接到 Amazon EC2 服务器

[英]How do i connect to Amazon EC2 Server with PHP and SSH

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

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

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

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

找到了解决方案,使用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"));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM