【问题标题】:How do i connect to Amazon EC2 Server with PHP and SSH如何使用 PHP 和 SSH 连接到 Amazon EC2 服务器
【发布时间】:2020-03-01 02:27:30
【问题描述】:

我无法使用 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 文件的任何其他服务器上运行终端命令。

【问题讨论】:

  • 我之前已经放弃了这样的任务,最终改用了 exec(ssh -t)。虽然这绝对不是您所期望的解决方案,但它可以暂时用作临时解决方法。

标签: php amazon-ec2 ssh


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 2018-10-31
    • 1970-01-01
    • 2012-06-13
    • 2013-04-26
    • 1970-01-01
    • 2018-03-25
    相关资源
    最近更新 更多