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!

  • I'm not sure if I understand you correctly, but could sshfs solve your problem? – cmende Oct 9 '15 at 8:35
  • 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... – Simone Bolognini Oct 9 '15 at 8:37
  • 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. – cmende Oct 9 '15 at 8:41
  • 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. – Faheem Mitha Oct 9 '15 at 9:00
  • 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 Oct 9 '15 at 9:03

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).

  • 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 Oct 9 '15 at 8:55
  • What is your computer to not be able to open two terminals ? – netmonk Oct 9 '15 at 9:10
  • @SimoneBolognini Then probably the last alternative is the most straightforward way to copy files (but note that since there is no tty allocated at address, password entry will not work and you have to use key based authentication at least between address and different_address; but key based authentication is preferred to passwords anyway). – Radovan Garabík Oct 9 '15 at 9:46
  • @SimoneBolognini what you call 'main page' should be probably called 'shell session' or 'shell prompt', or informally just 'shell' – Radovan Garabík Oct 9 '15 at 9:47

You can use scp :

scp file user@machine2:/home/user

or inverse

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

  • Yes but this would work only within machine1, and I want to do this without having to ssh manually to machine1 first. – Simone Bolognini Oct 9 '15 at 8:56
  • 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 – mazs Oct 9 '15 at 9:00
  • you can also setup a crontab shell script which does the scp for you, in that case you don't have to login – mazs Oct 9 '15 at 9:11
  • This answer is about basic scp usage, does not help the original problem - no direct connection between PC and machine2.. – Radovan Garabík Oct 9 '15 at 10:27

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.

  • 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? – Simone Bolognini Oct 9 '15 at 9:10
  • 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. – migrc Oct 9 '15 at 9:15
  • Exactly! This is the problem. – Simone Bolognini Oct 9 '15 at 9:17
  • I will look for the commands you will need to do this – migrc Oct 9 '15 at 9:19
  • @migrc the -L switch - see my previous answer – Radovan Garabík Oct 9 '15 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.