通过将私钥作为字符串提供给服务器来ssh到服务器-paramiko aws lambda问题

ssh to server by giving private key as string to - paramiko aws lambda issue

我正在尝试使用 python paramiko 包通过 ssh 连接到服务器。

当我尝试使用“pem”密钥通过 ssh 连接到服务器时,它成功了,但是当我尝试将私钥内容放入字符串中时,它显示错误。

下面是代码和输出:

import paramiko<br/>
import StringIO<br/>
content="-----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQE ----whatever content"<br/>
private_key = StringIO.StringIO(content)<br/>
k = paramiko.RSAKey.from_private_key(private_key)<br/>
c = paramiko.SSHClient()<br/>
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())<br/>
host="XX.XX.XX.XXX"<br/>
c.connect( hostname = host,username="ec2-user", pkey = k )<br/>
print "Connected to " + host`<br/>

输出:

Traceback (most recent call last):<br/>
  File "one.py", line 6, in <module><br/>
    k = paramiko.RSAKey.from_private_key(private_key)<br/>
  File "/home/ec2-user/abc/local/lib/python2.7/site-packages/paramiko/pkey.py", line 217, in from_private_key<br/>
    key = cls(file_obj=file_obj, password=password)<br/>
  File "/home/ec2-user/abc/local/lib/python2.7/site-packages/paramiko/rsakey.py", line 42, in __init__<br/>
    self._from_private_key(file_obj, password)<br/>
  File "/home/ec2-user/abc/local/lib/python2.7/site-packages/paramiko/rsakey.py", line 167, in _from_private_key<br/>
    data = self._read_private_key('RSA', file_obj, password)<br/>
  File "/home/ec2-user/abc/local/lib/python2.7/site-packages/paramiko/pkey.py", line 277, in _read_private_key<br/>
    raise SSHException('not a valid ' + tag + ' private key file')<br/>
paramiko.ssh_exception.SSHException: not a valid RSA private key file<br/>

任何人都可以提出可能是什么问题吗?

私钥文件是一个多行文件。

因此,当将其用作字符串时,请保留内容结构以保持其有效。

content = """-----BEGIN RSA PRIVATE KEY-----
              MIIEpgSIJOBAAKCAQEAqwH5fWIbtFRankLqvtnQ6OKwmIa49i
              ..........................................
              -----END RSA PRIVATE KEY-----"""

private_key = StringIO.StringIO(content)
k = paramiko.RSAKey.from_private_key(private_key)