简单的SFTP脚本

问题描述 投票:-1回答:2

我收到错误无法加载hostkeys。

你好,

我在编写一个脚本以将文件推送到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中创建文件夹,它指定将密钥放在那里,但它也不允许我。提前感谢您提供的任何见解。

python ssh sftp
2个回答
0
投票

抄写Charles Duffy的评论作为答案:

id_rsa.pub文件不是主机密钥。为了清楚起见,客户端使用主机密钥文件来确保它连接的主机是真实的(它是标识远程服务器的已知公钥的列表,而不是与识别用户的公共密钥有关的任何内容。在)。


0
投票

我能够使用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)
© www.soinside.com 2019 - 2024. All rights reserved.