此代碼會引發異常。如何驗證SSH指紋而不將其存儲在文件中?我相信下面的代碼是爲公鑰設計的。但是,使用SFTP服務器的客戶端驗證了指紋,並沒有爲我提供公鑰。Python - pysftp/paramiko - 使用其指紋驗證主機密鑰
import os
import shutil
import pysftp
import paramiko
connection_info = {
'server': "example.com",
'user': "user",
'passwd': "password",
'target_dir': "out/prod",
'hostkey': "ssh-rsa 2048 d8:4e:f1:f1:f1:f1:f1:f1:21:31:41:14:13:12:11:aa",
}
def move_files_from_server_to_local(server, localpath):
target_dir = server['target_dir']
keydata = "d8:4e:f1:f1:f1:f1:f1:f1:21:31:41:14:13:12:11:aa"
key = paramiko.RSAKey(data=decodebytes(keydata))
options = pysftp.CnOpts()
options.hostkeys.add('example.com', 'ssh-rsa', key)
with pysftp.Connection(
server['server'],
username=server['user'],
password=server['passwd'],
cnopts=options) as conn:
conn.get_d(target_dir, localpath)
delete_files_from_dir(conn, target_dir)
move_files_from_server_to_local(connection_info, "/")
該代碼基於Verify host key with pysftp。
尼斯。我從我的回答中鏈接了這個。 –