用于在远程服务器中创建 zip 文件的 Python 脚本 [英] Python script for creating zip file in remote server

查看:61
本文介绍了用于在远程服务器中创建 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")

这篇关于用于在远程服务器中创建 zip 文件的 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆