1

Can I use SFTP to untar a .tar.gz on a remote server?

def untarFile(self,fileName,hostname,username,key_filename):
  self.connect(hostname, username, key_filename)
  self.username = username
  self.key_filename = key_filename
  self.fileName = fileName
  sftp_client = self.ssh.open_sftp()
  if (fileName.endswith("tar.gz")):
    print "Extracting files"
    tar = sftp_client.tarfile.open(fileName)
    tar.extractall()
    tar.close()
    print "Files extracted to %s" %self.autoRemote
  else:
    print "Could not extract test code"

Error that I see:

Connection with server succesfully established with user test...
Extracting files
Traceback (most recent call last):
  File "test.py", line 181, in <module>
    ssh_obj.untarFile(ssh_obj.testCode, ssh_obj.hostname, ssh_obj.autouser, ssh_obj.autoSSHK)
  File "test.py", line 164, in untarFile
    tar = sftp_client.tarfile.open(fileName)
AttributeError: 'SFTPClient' object has no attribute 'tarfile'```
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
epicwork
  • 59
  • 10

1 Answers1

0

You cannot extract files on the remote server using SFTP protocol.

All you can do is to use SSH to execute a (shell) command on the server, that does what you need. Assuming you have a shell access to the server at all.

For example, if you are connecting to a *nix server, you can use execute a tar command there.

For executing a command using SSH in Python Paramiko, see:
Python Paramiko - Run command

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992