[英]Python paramiko.ssh_exception.SSHException: No existing session
[英]paramiko.ssh_exception.SSHException client.connect format
我正在嘗試登錄到遠程機器 (EC2)。 但它一直說有一個 SSHException 並且密鑰無效。
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()
而且,有沒有更好的方法將 SSH 到 EC2 與 python?
最終,感謝一位朋友,我找到了答案。 雖然這是我犯的一個簡單錯誤,但我將在這里提及它,因為很難追溯錯誤。
這是因為我將host=
作為user@host
。 如果有人需要,這是一個工作代碼。 用戶名通常是您與 AWS 一起使用的操作系統。 例如ubuntu
用於 Ubuntu。
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.