Unzip with Paramiko - Python

99封情书 提交于 2019-12-01 08:04:32

问题


I have a script to upload a zip file through sftp with the Paramiko module. I'm trying to unzip the zip file, but it's not working. I don't get any feedback that says it's not working.

import paramiko, re

spaceNeeded = 11534336
localpath = 'C:\\Users\\username\\Downloads\\10_Recommended.zip'
remotepath = '/tmp/10_Recommended.zip'
sudopass = "password"

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('host', username='username', password='password')
stdin, stdout, stderr =ssh.exec_command("df -k /tmp | grep /tmp | tr -s ' ' ',' | cut -d ',' -f4")
actualSpace = ''.join(stdout.readlines())

if actualSpace > spaceNeeded:
    transport = paramiko.Transport(('host',22))
    transport.connect(username="username", password="password")
    sftp = paramiko.SFTPClient.from_transport(transport)
    print "Starting upload"
    sftp.put(localpath, remotepath)
    stdin, stdout, stderr =ssh.exec_command("ls /tmp | grep 10_Recommended.zip")
    zipfile = ''.join(stdout.readlines())
    print "Unzipping file"

    stdin, stdout, stderr = ssh.exec_command("unzip /tmp/10_Recommended.zip")

回答1:


Your ssh connection may be closed before unzipping is done. I had a similar problem and added stdout.read() after exec_command to force the connection to be open until unzip is done.




回答2:


I know this is 3 months old, but I think I just fixed a similar issue and decided to contribute.

Try making the following change:

sftp = paramiko.SFTPClient.from_transport(transport)
print "Starting upload"
sftp.put(localpath, remotepath)
sftp.close() #this should block until put finishes
stdin, stdout, stderr =ssh.exec_command("ls /tmp | grep 10_Recommended.zip")



回答3:


Using tar replace unzip works for me.

I thought unzip is not pure Linux command.



来源:https://stackoverflow.com/questions/32012942/unzip-with-paramiko-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!