用于在远程服务器中创建 zip 文件的 Python 脚本

用于在远程服务器中创建 zip 文件的 Python 脚本

问题描述:

我想编写一个 python 脚本,该脚本连接到远程服务器并在远程服务器中创建一个 zip 文件,该文件由远程服务器本身中存在的特定文件组成.我已经编写了以下脚本:

I want to write a python script that connects to the remote server and creates a zip file in the remote server which consists of specific files present in remote server itself. I have written the below script :

c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect('15.100.1.1', username='user', password='123')
sftp=c.open_sftp()
c.exec_command("cd 'C:\\Program Files\\temp'")
filenames = ['a.txt','b.txt','c.txt']
zf = zipfile.ZipFile('files.zip', mode='w')
for fname in filenames:
    zf.write(fname)
zf.close()
sftp.close()
c.close()

但不是在远程服务器中创建 zip 文件,而是在本地计算机本身中创建 zip 文件.任何人都可以请帮助我.....

But instead of creating zip file in remote server, the zip file gets created in the local machine itself. Can anyone please help me in this.....

正如 UlfR 所说,尝试直接在远程机器上进行压缩".既然他(她?)没有说怎么做,我建议像

As UlfR says, "Try to do the zipping directly on the remote machine".  Since he (she?) didn't say how to do that, I suggest something like

c.exec_command("zip files.zip a.txt b.txt c.txt")