paramiko.sh_exception.SSHException客户端连接格式



我正在尝试登录到远程机器(EC2(。但它一直在说存在SSHException,并且密钥是Invalid。

paramiko.ssh_exception.SSHException: Invalid key (class: RSAKey, data type: oQIBAAKCAQEApkTX3as35p1TF9W..............

这是我的代码:

import paramiko
amznKey = "MIIEoQIBAAKCAQEApkTX3as35p1TF9W............."
key = paramiko.RSAKey(data=bytes(amznKey, 'utf-8'))
client = paramiko.SSHClient()
client.get_host_keys().add('ubuntu@ec2-3-123-12-80.us-east-2.compute.amazonaws.com', 
'ssh-rsa', key)
client.connect('ubuntu@ec2-2-134-99-80.us-east-2.compute.amazonaws.com', username='', password='')
stdin, stdout, stderr = client.exec_command('ls')
for line in stdout:
print('... ' + line.strip('n'))
client.close()

还有,有没有更好的方法用python将SSH连接到EC2?

多亏了一位朋友,我终于找到了答案。虽然这是我犯的一个简单的错误,但我要在这里提到它,因为很难追溯错误。

这是因为我把host=作为user@host。如果有人需要,这里有一个工作代码。用户名通常是你在AWS中使用的操作系统。例如用于Ubuntu的CCD_ 3。

import paramiko

hostname = "ec2-3-123-12-80.us-east-2.compute.amazonaws.com"  # Remote machine's public DNS
username = "ubuntu"  # Username for SSH                                     
pass_key = "amzonLinux16.pem"  # Your Private Key for AWS EC2
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
client.connect(hostname, username=username, key_filename=pass_key)
for command in 'echo "Hello, world!"', 'cat ~/test', 'uptime', 'ifconfig':
stdin, stdout, stderr = client.exec_command(command)
stdin.close()
print(stdout.read().decode('utf-8'))
stdout.close()
stderr.close()
client.close()

最新更新