【发布时间】:2021-03-27 23:16:31
【问题描述】:
我的代码使用 Paramiko 登录到远程 Unix 主机
-
grep远程 Linux 主机上的字符串,然后将结果写入新文件/tmp/file.<timestamp>.txt。 – 这很好用 - sftp 到远程 Linux 主机并获取我刚刚从我的
grep创建的文件。 – 这是我遇到问题的步骤。
远程系统 = Linux 本地系统 = Windows
问题是当我从我的 Unix 主机 sftp 获取文件时,一旦它到达我的 Windows 系统,该文件就是空的。
我可以执行创建文件的命令并一次性执行 SFTP 吗?如果是这样,在将其放在本地 Windows 系统上的特定目录中时如何执行此操作的任何示例?或者我需要处理第一个命令中的 stdout 吗?虽然我认为这行不通。
butler_report_dir = Path.home() / 'butler_reports'
butler_report_dir.mkdir(exist_ok=True)
req_date = req_date.strftime('%Y%m%d')
req_date = '*' + req_date + '*'
r_cat_file = '/tmp/cat_report_' + timestamp + '.txt'
ssh_client = SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(
ssh_server,
username=ssh_username,
password=ssh_passwd,
look_for_keys=False,
)
cmd_stmt = f'cd {old_arch_dir};' f'/usr/bin/gzip -cd {req_date} | grep {orderid} > {r_cat_file}'
stdin, stdout, stderr = ssh_client.exec_command(cmd_stmt)
# The Code Above seems to work fine as my file is being created with the timestamp
# This part below is where I am having the issue
file = butler_report_dir / 'cat_file.txt'
ftp_client = ssh_client.open_sftp()
stdin.close()
stdout.close()
stderr.close()
ssh_client.close()
【问题讨论】: