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

查看:251
本文介绍了“无法加载主机密钥".使用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:\Users\Rohan\.ssh\known_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 a 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天全站免登陆