Paramiko - 使用私钥连接 - 不是有效的 OPENSSH 私钥/公钥文件 [英] Paramiko - connect with private key - not a valid OPENSSH private/public key file

查看:129
本文介绍了Paramiko - 使用私钥连接 - 不是有效的 OPENSSH 私钥/公钥文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力寻找解决方案,但无法理解我做错了什么.

I am trying to find the solution to this and can't understand what I'm doing wrong.

在我的 Linux 服务器上,我运行了以下命令:

On my Linux server I have run the following command:

ssh-keygen -t rsa

这生成了 id_rsaid_rsa.pub 文件.

This generated an id_rsa and id_rsa.pub file.

然后我将它们都复制到本地并尝试运行以下代码:

I then copied them both locally and attempted to run the following code:

    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')

我的日志中出现以下错误:

I get the following error in my log:

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 私钥文件.

am I missing something? It is a valid OPENSSH private key file.

edit - 如果我使用 id_rsa.pub 我得到以下内容:

edit - if I use the id_rsa.pub I get the following:

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

为什么?

推荐答案

我正在运行 Paramiko RSA 密钥身份验证设置.以下是我所做的总结:

I have a Paramiko RSA key authentication setup running. Here is a summary of what I did:

  • 运行 ssh-keygen -t rsa 以生成 id_rsa 和 id_rsa.pub 文件

  • run ssh-keygen -t rsa to generate the id_rsa and id_rsa.pub files

将 id_rsa.pub 的内容复制到 ~/.ssh/authorized_keys(在目标系统)

copy contents of id_rsa.pub into ~/.ssh/authorized_keys (on the target system)

将 id_rsa(私有)密钥文件复制到客户端机器上

copy the id_rsa (private) keyfile onto the client machine

(在目标上我的 .ssh/模式为 755,authorized_keys 模式为 644)

(on the target I have mode 755 on .ssh/ and 644 on authorized_keys)

以下代码使用 Paramiko 运行登录:

The following code runs a login using 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 文件中看到以下内容:

The following is seen in the app.log file:

    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 上.

(NB: The Paramiko client is using the private key file.) This is all on Python 2.7.

这篇关于Paramiko - 使用私钥连接 - 不是有效的 OPENSSH 私钥/公钥文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆