[英]With SSH2 and phpseclib, how do I upload a file to Amazon EC2 server in PHP?
[英]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.