Copy and get files from one machine which is connected via ssh to another one

8,671

Solution 1

I am confused by the 'main page' term and the usernames in the form of 'machineX'. And I do not think there really is a need to use python.

My solution would be:

Establish a ssh tunnel to machine2:

mycomputer$ ssh -L2222:different_address:22 machine1@address

Now you can copy files through the tunnel, or use sshfs or rsync or whatever you need:

mycomputer$ scp -P 2222 machine2@address:/whatever /target_local_dir

Alternative would be to run scp from the address computer, assuming there is sshd running on mycomputer, or just chain the ssh commands and cat the file you need:

ssh machine1@address ssh machine2@different_address cat /path_to_the_remote_file > /target_local_file

(distribute ssh keys as necessary).

Solution 2

You can use scp :

scp file user@machine2:/home/user

or inverse

scp user@machine2:/home/user/file /local/directory

Solution 3

Try to open a ssh tunnel from machine1 to your host

ssh user1@machine1-ip-address -p machine1-ssh-port -L local-port:machine2-ip-address:machine2-ssh-port -N

For example

ssh user1@machine1 -p 22 -L 8181:machine2:22 -N

After executing this in one terminal it will ask for machine1 password. You should not close this terminal because you will close the tunnel.

Now open a second terminal and you should be able to ssh machine2 from your machine. The command should be

ssh user2@machine2 -p 8181

With this you will have ssh access to machine2 from your local machine.

Share:
8,671

Related videos on Youtube

Simone Bolognini
Author by

Simone Bolognini

Updated on September 18, 2022

Comments

  • Simone Bolognini
    Simone Bolognini over 1 year

    I read a lot of answers to questions very similar to this one, but I couldn't find something that really fits my problem. Here's the situation. I have a machine whose address is "machine1@address", and from this one I reach "machine2@different_address". Now, I have to send/get files to/from machine2 directly from/on my computer, because I don't want to occupy memory "permanently" on machine1.

    My goal is to automate this process by writing a Python script. So, the best solution would be to avoid to open (I don't really know how to call it, I hope you'll understand) the 'main page' of machine1, since the Python script would stop running until I close it (at least, this is what happens when I simply use ssh).

    Thank you!

    • cmende
      cmende over 8 years
      I'm not sure if I understand you correctly, but could sshfs solve your problem?
    • Simone Bolognini
      Simone Bolognini over 8 years
      I know, my explanation is probably a little confused. Do not care about the python stuff.. I just want to get and send files or directories from/to machine2 (where machine2@different_address makes sense only within machine1) just by typing the passwords and doing nothing more. Does sshfs give me this possibility? I never used it...
    • cmende
      cmende over 8 years
      With sshfs you can access remote directories by mounting them locally. I.e. you can mount machine2@different_address:/home/user/foo to your local /home/user/bar and thus access files on machine2@different_address:/home/user/foo as if they were local in /home/user/bar.
    • Alen Milakovic
      Alen Milakovic over 8 years
      The question is not clear. How many different machine are you referring to? And where do you want to transfer files to and from? And you don't want to be logged into either of the two machines that you are transferring files to/from? At the time of writing this comment, there are three answers recommending scp, but you're better off using rsync.
    • Simone Bolognini
      Simone Bolognini over 8 years
      There are 2 remote machines and my personal computer. I want to transfer files from machine2 to my computer and yes, I don't want to be logged in the two machines. I just want to run a script from my computer, enter my password, and send the files directly to machine2 and, in reverse, run a script on my computer and get the files on my computer from machine2. Again, machine2@different_address makes sense only when typed within machine1.
  • Simone Bolognini
    Simone Bolognini over 8 years
    When you enter remotly a computer with ssh then your shell switches to the remote shell of the computer you'r accessing (I'm absolutely not an expert in this things so I'm probably using the wrong words to express myself).. this is what I mean by 'main page'. And running your first command I actually enter this main page and this is not what I want to do. It would be nice (I mean, if it is possible) to just have to type the password and copy the file in machine2. (and yes, machine1 and machine2 as username was actually a bad choice)
  • Simone Bolognini
    Simone Bolognini over 8 years
    Yes but this would work only within machine1, and I want to do this without having to ssh manually to machine1 first.
  • magor
    magor over 8 years
    would 'rsync' help you ? it works nicely between linux machines, in case you need to sync files with a windows machine as well, you might need to mount the windows folder to a linux machine
  • netmonk
    netmonk over 8 years
    What is your computer to not be able to open two terminals ?
  • Simone Bolognini
    Simone Bolognini over 8 years
    This would be perfect, but it is not working when I want to fetch files from machine2. machine2@different_address works only within machine1, and I would like to do everything on my computer without logging directly on machine1 to type the commands. To do the opposite, instead, works fine. Any clue?
  • magor
    magor over 8 years
    you can also setup a crontab shell script which does the scp for you, in that case you don't have to login
  • migrc
    migrc over 8 years
    Ok. Now I understand. You only has access to machine2 from machine1, is this correct? Maybe if you open a ssh tunnel to machine1 and you redirect the IP:port for ssh-ing machine2 to a local port in your machine.
  • Simone Bolognini
    Simone Bolognini over 8 years
    Exactly! This is the problem.
  • migrc
    migrc over 8 years
    I will look for the commands you will need to do this
  • Radovan Garabík
    Radovan Garabík over 8 years
    @SimoneBolognini what you call 'main page' should be probably called 'shell session' or 'shell prompt', or informally just 'shell'
  • Radovan Garabík
    Radovan Garabík over 8 years
    @migrc the -L switch - see my previous answer
  • migrc
    migrc over 8 years
    @SimoneBolognini If you have doubts about my explanation, ask me, please.
  • Radovan Garabík
    Radovan Garabík over 8 years
    This answer is about basic scp usage, does not help the original problem - no direct connection between PC and machine2..
  • Simone Bolognini
    Simone Bolognini over 8 years
    Thank you everyone. I found a way using the pxssh python package. This enables you to open an ssh connection without switching to the machine1 shell session. In this way, still using python, I'm able to connect directly to machine2 without much trouble. If you want, I can post my code below.