“无法加载主机密钥"使用 pysftp 连接到 SFTP 服务器时出现警告 [英] "Failed to load HostKeys" warning while connecting to SFTP server with pysftp

查看:37
本文介绍了“无法加载主机密钥"使用 pysftp 连接到 SFTP 服务器时出现警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 Python 脚本来使用密钥身份验证连接到 SFTP 服务器.它成功连接到服务器,但显示以下警告(见下文).它是什么意思以及如何删除它.必须对代码进行哪些更改?

I wrote a Python script to connect to SFTP server using key authentication. It connects to server successfully but shows the following warning (see below). What does it mean and how to remove it. What changes has to made in code?

我的代码:

import os
import pysftp
import socket
import paramiko
import time
import os.path
import shutil

IP = "127.0.X.X"
myUsername = "USERNAME"
port = 22

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

import os
privatekeyfile = os.path.expanduser("C:\Users\Rohan\.ssh\cool.prv")
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)

try:
    with pysftp.Connection(host=IP, username=myUsername,private_key=mykey,cnopts=cnopts) as sftp:
        try:
            r=str(socket.gethostbyaddr(IP))
            print("connection successful with "+r)

        except socket.herror:
            print("Unknown host")
except:
    print("connection failed")

警告:

UserWarning: Failed to load HostKeys from C:UsersRohan.sshknown_hosts.  You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
  warnings.warn(wmsg, UserWarning)

推荐答案

我认为这是 pysftp 中的一个错误.每次使用 cnopts.hostkeys = None 时都会得到这个(尽管警告实际上建议使用它).

I believe it's a bug in pysftp. You get this everytime you use cnopts.hostkeys = None (despite the warning actually suggesting to use that).

无论如何,您不应该使用cnopts.hostkeys = None,这样做会失去安全性.
有关正确的解决方案,请参阅使用 pysftp 验证主机密钥.

Anyway, you should not use cnopts.hostkeys = None, you lose security by doing so.
For the correct solution, see Verify host key with pysftp.

通过您提到的密钥验证,我假设您将帐户密钥误认为是主机密钥.阅读我关于 SSH 密钥对的文章以了解区别.

By your reference to key authentication, I assume you mistake your account key with host key. Read my article about SSH key pairs to understand the difference.

这篇关于“无法加载主机密钥"使用 pysftp 连接到 SFTP 服务器时出现警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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