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")
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.
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")
你的好朋友霍格
Using tar
replace unzip
works for me.
I thought unzip
is not pure Linux command.
来源:https://stackoverflow.com/questions/32012942/unzip-with-paramiko-python