我正在努力找到解决方案,并且无法理解我做错了什么。
在我的linux服务器上,我运行了以下命令:
ssh-keygen -t rsa
这会生成id_rsa
和id_rsa.pub
文件。
然后我在本地复制它们并尝试运行以下代码:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('myserver', username='myuser', key_filename='id_rsa')
sftp = ssh.open_sftp()
sftp.chdir('/path/to/file')
sftp.get(file, os.path.join(tmp_dir, '{0}.existing-{1}'.format('myfile', current_datetime)))
except Exception, err:
logging.debug(err)
logging.info('Error connecting to Host')
我的日志中出现以下错误:
2017-08-22 22:41:54,486 Switch to new keys ...
2017-08-22 22:41:54,502 Adding ssh-ed25519 host key for myserver.domain.com: 51ac2fe875499371256dd8c5a132f394
2017-08-22 22:41:54,502 Trying key 7688e32d30edb2c94bfe39be9897004f from id_rsa
2017-08-22 22:41:54,532 userauth is OK
2017-08-22 22:41:54,549 Authentication (publickey) failed.
2017-08-22 22:41:54,563 not a valid OPENSSH private key file
2017-08-22 22:41:54,563 Error connecting to Host
2017-08-22 22:41:54,657 EOF in transport thread
我错过了什么吗?它是一个有效的OPENSSH私钥文件。
编辑 - 如果我使用id_rsa.pub
,我会得到以下内容:
2017-08-22 22:58:09,631 Kex agreed: ecdh-sha2-nistp256
2017-08-22 22:58:09,631 HostKey agreed: ssh-ed25519
2017-08-22 22:58:09,631 Cipher agreed: aes128-ctr
2017-08-22 22:58:09,631 MAC agreed: hmac-sha2-256
2017-08-22 22:58:09,631 Compression agreed: none
2017-08-22 22:58:09,694 kex engine KexNistp256 specified hash_algo <built-in function openssl_sha256>
2017-08-22 22:58:09,710 Switch to new keys ...
2017-08-22 22:58:09,726 Adding ssh-ed25519 host key for myserver.domain.com: 51ac2fe875499371256dd8c5a132f394
2017-08-22 22:58:09,726 not a valid OPENSSH private key file
2017-08-22 22:58:09,726 Error connecting to Host
2017-08-22 22:58:09,819 EOF in transport thread
第w?
答案 0 :(得分:5)
我正在运行paramiko rsa密钥身份验证设置,以下是我所做的总结:
运行ssh-keygen -t rsa生成id_rsa和id_rsa.pub文件
将id_rsa.pub的内容复制到〜/ .ssh / authorized_keys(在 目标系统)
将id_rsa(私有)密钥文件复制到客户端计算机上
以下代码使用paramiko运行登录:
import logging
import paramiko
logger = paramiko.util.logging.getLogger()
hdlr = logging.FileHandler('app.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
k = paramiko.RSAKey.from_private_key_file('id_rsa')
ssh.connect('160.100.28.216', username='edwards', pkey = k)
sftp = ssh.open_sftp()
sftp.chdir('/home/edwards')
except Exception, err:
logging.debug(err)
logging.info('Error connecting to Host')
以下内容见app.log文件:
2017-08-23 16:52:33,154 INFO Connected (version 2.0, client OpenSSH_6.6.1)
2017-08-23 16:52:46,926 INFO Authentication (publickey) successful!
2017-08-23 16:52:47,203 INFO [chan 0] Opened sftp connection (server version 3)
(注意paramiko客户端正在使用私钥文件。)这完全在Python 2.7上。我希望其中的部分或全部可能有用。
答案 1 :(得分:3)
您可以使用ssh-keygen将id_rsa转换为RSA类型的私钥。我遇到了类似的情况,并且对我有用。
要将“ BEGIN OPENSSH私钥”转换为“ BEGIN RSA私钥”
ssh-keygen -p -m PEM -f ~/.ssh/id_rsa