简体   繁体   English

PHP SSH2和Amazon EC2 .pem

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

I've been looking for a way to use PHP's SSH2 to create a sort of terminal. 我一直在寻找一种使用PHP的SSH2创建终端的方法。 To connect to Amazon in a normal terminal, you would use something like ssh -i path_to/key.pem ec2.ip-555-xxx.com . 要在普通终端中连接到Amazon,您可以使用ssh -i path_to/key.pem ec2.ip-555-xxx.com In PHP on the other hand, SSH2 has a function ssh2_auth_pubkey_file . 另一方面,在PHP中,SSH2具有功能ssh2_auth_pubkey_file But have run into a bit of a wall here, as Amazon only provides me with 1 private key (.pem) file, and the function has arguments for both private and public keys. 但是这里有些麻烦,因为Amazon仅向我提供了1个私钥(.pem)文件,并且该函数同时具有私钥和公钥的参数。 Ultimately I'd like to have a client upload a .pem file to the server and be able to connect to a local or remote SSH server with PHP SSH2 on Amazon using that .pem file. 最终,我希望客户端将.pem文件上传到服务器,并能够使用该.pem文件通过Amazon上的PHP SSH2连接到本地或远程SSH服务器。

.pem is the server certificate for the apache web server, it has nothing to do with ssh. .pem是apache Web服务器的服务器证书,与ssh无关。 See: https://serverfault.com/questions/9708/what-is-a-pem-file-and-how-does-it-differ-from-other-openssl-generated-key-file-f 参见: https : //serverfault.com/questions/9708/what-is-a-pem-file-and-how-does-it-differ-from-other-openssl-generated-key-file-f

Seems it might also be a combo file with public and private keys in it. 似乎它也可能是其中包含公钥和私钥的组合文件。 In any case it will not work for ssh directly, you will need to convert it to normal files. 无论如何,它都不能直接用于ssh,您需要将其转换为普通文件。

You have it backward anyway - amazon will not give you the private key, quite the opposite - you give amazon the public key. 无论如何,您都将其向后-亚马逊不会给您私有密钥,相反,您会给亚马逊提供公共密钥。 You generate the private/public key pair locally, then upload the public key into the .ssh/authorize_keys files. 您在本地生成私钥/公钥对,然后将公钥上载到.ssh / authorize_keys文件中。

Personally, I'd recommend phpseclib, a pure PHP SSH2 implementation be used: 就个人而言,我建议使用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');
?>

This is how you get the private and public keys from .pem within php 这是您从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