【问题标题】:Simple SFTP script简单的 SFTP 脚本
【发布时间】:2019-04-20 16:14:16
【问题描述】:

我收到错误,无法加载主机密钥。

你好,

我在编写用于将文件推送到 SFTP 的脚本时遇到问题。我正在使用 Windows,下面是我到目前为止的代码。实际的数据操作和命名更改工作正常 - 它在 SFTP 部分中断。我希望你们中的一位大师可以帮助新手。

import pandas as pd
import datetime
import pysftp
import sys

today = str(datetime.date.today().strftime("%m%d%y"))

report = pd.read_csv('C:\\Users\\nickkeith2\\PycharmProjects\\clt\\041719_clt_Facility_company_Inv.csv')

report.columns = report.columns.str.replace('_', ' ')

report.to_csv('C:\\Users\\nickkeith2\\PycharmProjects\\clt\\' + today + '_clt_Facility_company_Inv2.csv',
              index=False)

remote_file = 'C:\\Users\\nickkeith2\\PycharmProjects\\clt\\' + today + '_clt_Facility_company_Inv2.csv'[1]

cnopts = pysftp.CnOpts()
cnopts.hostkeys.load("C:\\Users\\nickkeith2\\id_rsa.pub")

srv = pysftp.Connection(host="xx.xxx.xxx.xxx", username="sftpuser")

srv.put(remote_file)

srv.close()

print(report.columns)

我尝试了使用密钥、不使用密钥和使用密码的各种组合 - 但无论它返回错误是什么:

UserWarning: Failed to load HostKeys from C:\Users\nickkeith2\.ssh\known_hosts.  You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
  warnings.warn(wmsg, UserWarning)

我试图在 Windows 中创建文件夹,它指定将密钥放在那里,但它也不允许我这样做。提前感谢您提供的任何见解。

【问题讨论】:

  • id_rsa.pub 文件不是主机密钥。
  • 明确地说,客户端使用主机密钥文件来确保它所连接的主机是真实的(它是一个已知公钥列表标识远程服务器,与公钥标识正在登录的用户没有任何关系)。
  • 谢谢 - 回到绘图板。
  • Charles - 在线阅读后我看到尝试:cnopts = pysftp.CnOpts() cnopts.hostkeys = None - 但这给了我同样的错误,有什么建议吗?

标签: python ssh sftp


【解决方案1】:

抄录 Charles Duffy 的 cmets 作为答案:

id_rsa.pub 文件不是主机密钥。需要明确的是,客户端使用主机密钥文件来确保它连接的主机是真实的(它是一个已知公钥列表标识远程服务器,与公钥标识正在登录的用户)。

【讨论】:

    【解决方案2】:

    我能够使用 paramiko 而不是 PYSFTP 成功完成此操作。

    谢谢!

    import pandas as pd
    import datetime
    import paramiko
    
    
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    
    today = str(datetime.date.today().strftime("%m%d%y"))
    
    report = pd.read_csv('C:\\Users\\nickkeith2\\PycharmProjects\\clt\\041719_marshall_Facility_clt_Inv.csv')
    
    report.columns = report.columns.str.replace('_', ' ')
    
    report.to_csv('C:\\Users\\nickkeith2\\PycharmProjects\\clt\\' + today + '_marshall_Facility_clt_Inv2.csv',
                  index=False)
    
    remote_file = 'C:\\Users\\nickkeith2\\PycharmProjects\\clt\\' + today + '_marshall_Facility_clt_Inv2.csv'
    
    ssh_client.connect(hostname="xxxxxxxx", username="xxxxxxxx", password="xxxxxxxxxx")
    
    ftp_client=ssh_client.open_sftp()
    ftp_client.put(remote_file, '\\Outbound\\'+ today + '_marshall_Facility_clt_Inv2.csv')
    ftp_client.close()
    
    print(report.columns)
    

    【讨论】:

    • 当实际将代码投入生产时,它被拒绝,因为在尝试将其推送到客户端的 SFTP 时找不到文件。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多