Extract file from remote ssh host

4.8k views Asked by At

I have server2 with an file "/home/test.tar.gz". On server1 i want to execute command via ssh to extract test.tar.gz to local directory.

Something like:

 sshpass -p 'password' ssh root@server2 "tar zxf /home/test.tar.gz" > /home/try

Is this even possible?

3

There are 3 answers

0
Kasun Rathnayaka On

1st you should run ssh-copy-id or manually copy your ssh public key and share your server1 key to server2 and setup access without password.

follow below site :

http://www.thegeekstuff.com/2008/11/3-steps-to-perform-ssh-login-without-password-using-ssh-keygen-ssh-copy-id/

then use below command to unzip file using ssh

ssh -i /home/user/.ssh/user.pem user@server2 'tar -xzvf /home/user/test.tar.gz'
0
Andrea De Gaetano On

You should: - use ssh-copy-id to exchange key between first and second server in order to avoid the need to pass password.

Than you can: - use SCP to copy the tar from server 2 and extract it with tar Or - extract the tar on the second machine in a directory and than copy that directory with scp -R

1
AudioBubble On

You cannot redirect them to a single file as you are extracting multiple types of files.

In case you are trying to extract to a directory then.

cd /tmp/try
ssh server2 "cat /home/test.tar.gz" | tar xvzf -