Copy and get files from one machine which is connected via ssh to another one
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.
Related videos on Youtube
Simone Bolognini
Updated on September 18, 2022Comments
-
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 over 8 yearsI'm not sure if I understand you correctly, but could sshfs solve your problem?
-
Simone Bolognini over 8 yearsI 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 over 8 yearsWith 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 over 8 yearsThe 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 over 8 yearsThere 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 over 8 yearsWhen 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 over 8 yearsYes but this would work only within machine1, and I want to do this without having to ssh manually to machine1 first.
-
magor over 8 yearswould '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 over 8 yearsWhat is your computer to not be able to open two terminals ?
-
Simone Bolognini over 8 yearsThis 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 over 8 yearsyou can also setup a crontab shell script which does the scp for you, in that case you don't have to login
-
migrc over 8 yearsOk. 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 over 8 yearsExactly! This is the problem.
-
migrc over 8 yearsI will look for the commands you will need to do this
-
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 over 8 years@migrc the -L switch - see my previous answer
-
migrc over 8 years@SimoneBolognini If you have doubts about my explanation, ask me, please.
-
Radovan Garabík over 8 yearsThis answer is about basic scp usage, does not help the original problem - no direct connection between PC and machine2..
-
Simone Bolognini over 8 yearsThank 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.