如何在Python中从多个远程服务器读取多个文件?

问题描述 投票:0回答:1

例如,我有一个AWS ubuntu服务器列表

[email protected]
[email protected]
[email protected]
...

在这些服务器中的每一个上,我都有一个文件夹,文件数量可变,每个服务器的路径都相同,例如/roth/files/

我想编写一个Python脚本,该脚本将获取这些文件的内容并在我的计算机上本地合并它们。

我该如何在远程服务器上获取那些文件的内容?

我登录这些服务器的方式是

ssh -i  path/aws.pem [email protected]

例如,使用键

我在类似问题here上找到了答案

sftp_client = ssh_client.open_sftp()
remote_file = sftp_client.open('remote_filename')
try:
    for line in remote_file:
        # process line
finally:
    remote_file.close()

但是我看不到您在哪里提供服务器名称和密钥...

python amazon-web-services remote-access
1个回答
0
投票
aws_host_list = [] # Your list here

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

for host in aws_host_list:
    client.connect(<IP Address>, username=<User Name>, key_filename=<.PEM File path)
    sftp = client.open_sftp()
    sftp.put('/roth/files/', <Destination>)
    sftp.close()
    client.close()

# Then process you files
© www.soinside.com 2019 - 2024. All rights reserved.