Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unzip with Paramiko - Python

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")
like image 872
Brock Avatar asked Feb 10 '26 09:02

Brock


1 Answers

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.

like image 56
Kotaro Avatar answered Feb 13 '26 00:02

Kotaro