繁体   English   中英

PHP SSH2和Amazon EC2 .pem

[英]PHP SSH2 and Amazon EC2 .pem's

我一直在寻找一种使用PHP的SSH2创建终端的方法。 要在普通终端中连接到Amazon,您可以使用ssh -i path_to/key.pem ec2.ip-555-xxx.com 另一方面,在PHP中,SSH2具有功能ssh2_auth_pubkey_file 但是这里有些麻烦,因为Amazon仅向我提供了1个私钥(.pem)文件,并且该函数同时具有私钥和公钥的参数。 最终,我希望客户端将.pem文件上传到服务器,并能够使用该.pem文件通过Amazon上的PHP SSH2连接到本地或远程SSH服务器。

.pem是apache Web服务器的服务器证书,与ssh无关。 参见: https : //serverfault.com/questions/9708/what-is-a-pem-file-and-how-does-it-differ-from-other-openssl-generated-key-file-f

似乎它也可能是其中包含公钥和私钥的组合文件。 无论如何,它都不能直接用于ssh,您需要将其转换为普通文件。

无论如何,您都将其向后-亚马逊不会给您私有密钥,相反,您会给亚马逊提供公共密钥。 您在本地生成私钥/公钥对,然后将公钥上载到.ssh / authorize_keys文件中。

就个人而言,我建议使用phpseclib,这是一个纯PHP SSH2实现

<?php
include('Net/SSH2.php');

$key = new Crypt_RSA();
//$key->setPassword('whatever');
$key->loadKey(file_get_contents('privatekey'));

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', $key)) {
    exit('Login Failed');
}

echo $ssh->exec('ls -la');
?>

这是您从php中的.pem获取私钥和​​公钥的方法

$eKey = file_get_contents('/pathto/key.pem');
$key_private = openssl_get_privatekey($eKey);
$keyDet=openssl_pkey_get_details($key_private);
$key_public = openssl_pkey_get_public(array($keyDet['key'],""));
$keyPDet=openssl_pkey_get_details($key_public);

暂无
暂无

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

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