【发布时间】:2019-10-24 13:38:28
【问题描述】:
我编写了一个 Python 脚本来使用密钥身份验证连接到 SFTP 服务器。它成功连接到服务器,但显示以下警告(见下文)。这是什么意思以及如何删除它。需要对代码进行哪些更改?
我的代码:
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)
【问题讨论】:
标签: python python-3.x ssh paramiko pysftp