1

I need to write Python code to login to remote server and navigate to Zip file path and then unzip & save on remote server. As a next step, I need to access the files inside the unzipped folder. Can anyone please help. I referred few links but unable to get complete solution. https://medium.com/@keagileageek/paramiko-how-to-ssh-and-file-transfers-with-python-75766179de73

furas
  • 134,197
  • 12
  • 106
  • 148
  • Basically, you need to download archive, unzip it and upload decompressed content back to server. Or you need to upload decompression script to server and execute it. – Olvin Roght Jul 29 '21 at 16:06
  • Need not download it to my local, I want to unzip and save within the remote server and then read the files from the unzipped folder – mohd aamer hussain Jul 29 '21 at 16:25
  • you have to upload `.zip` file on server (using command `put`) and unzip it on server (using external program `unzip`). OR if there is no program `unzip` then you can unzip it on local computer and upload file by file using `put` – furas Jul 29 '21 at 16:56

1 Answers1

1

First you can upload file using .put() and next you can use external program unzip to uncompress it.

Something similar to

import paramiko

# login
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='192.168.0.1', username='user', password='PaSsWoRd')

# upload
ftp_client = ssh_client.open_sftp()
ftp_client.put('local.zip', 'remote.zip')
ftp_client.close()

# unzip
stdin, stdout, stderr = ssh_client.exec_command('unzip remote.zip')
print(stdout.read().decode())

# access files
stdin, stdout, stderr = ssh_client.exec_command('ls')
print(stdout.read().decode())

If you want to put in subfolder then you may need to create this subfolder

stdin, stdout, stderr = ssh_client.exec_command('mkdir upload')

ftp_client.put('local.zip', 'upload/remote.zip')

stdin, stdout, stderr = ssh_client.exec_command('cd upload ; unzip remote.zip')

stdin, stdout, stderr = ssh_client.exec_command('ls upload')
# or 
stdin, stdout, stderr = ssh_client.exec_command('cd upload ; ls')
furas
  • 134,197
  • 12
  • 106
  • 148
  • Tried this , print statement after ssh_client.exec_command('unzip remote.zip') doesn't print anything – mohd aamer hussain Jul 29 '21 at 17:29
  • maybe you already have files and `unzip` asks if overwrite them. You can use `unzip -y remote.zip` to answer `yes` for all questions. – furas Jul 29 '21 at 17:31
  • or it may need `unzip -o remote.zip` to `overwrite`. – furas Jul 29 '21 at 17:39
  • Thanks furas, above solution worked. I saved unzipped files to a folder. These are actually XMLs. Now my next step is store parse each xml using Element tree. How can i pass these XML files to the ET.parse() method? I tried solution from https://stackoverflow.com/questions/1596963/read-a-file-from-server-with-ssh-using-python but it doesn't work. I need to send these unzipped XMLs to ET.parse() – mohd aamer hussain Jul 29 '21 at 19:11
  • 1
    I don't know what you have in XML but you would have to send script on server and run it on server. And if you want to run code on local computer, read data from server and send result back to server then I think it is waste of time - you could do it directly on local computer and send only result to server.. – furas Jul 29 '21 at 19:49