ssh copy trick?

Story: Tip: OpenSSH Speed Tips and TricksTotal Replies: 13
Author Content
Sander_Marechal

Nov 22, 2008
5:15 PM EDT
Nice tricks Carla, but here's one that I am still looking for and which your article didn't list:

Suppose you're in an ssh shell to some server, from there in an ssh shell to another server and then again yo some third server. Now I see a file that I'd like to copy to my computer. How do I do that?

Right now I open up an extra shell and then manually copy the file from server3 to server2 to server1 and then to my computer, but there *has* to be an easier way...
tracyanne

Nov 22, 2008
6:08 PM EDT
Carla ssh [remote user]@host2 ls /home/[some user] won't work on my LAN, as I have log in as root disabled.

You will always have to log in as the remote user, then switch user to root, before you can ls some arbitrary user's home directory.
Sander_Marechal

Nov 22, 2008
7:44 PM EDT
Or you simply log in as the user who's home directory you want to see.
tracyanne

Nov 22, 2008
8:42 PM EDT
Quoting:Or you simply log in as the user who's home directory you want to see.


Of course. The point is the trick won't work unless you enable login as root, which is not recommended.
tuxchick

Nov 22, 2008
8:46 PM EDT
No TA, you don't need to login as root. This is what the example command looks like:

carla@host1:~$ ssh terry@host2 ls ~

Running ls this way lists carla's homedir, if she has one on the remote PC. According to shmget in the other thread, this is a Bash-ism, rather than a SSH quirk.
herzeleid

Nov 22, 2008
9:26 PM EDT
I get the same result whether I say:

ssh host ls

or the more wordy:

ssh user@host ls ~

In either case it lists my home directory on host.

Since I try to avoid extra typing, I never type redundant default arguments ;)
Sander_Marechal

Nov 22, 2008
11:37 PM EDT
@herzeleid: But people need to understand *why* the `ssh host ls` list your home directory.

1) ssh defaults to the user you are logged in at on the local machine. So `ssh host` becomes `ssh you@host`.

2) When you log in with `ssh you@host` then you end up in the home directory of user "you", that is /home/you

3) The `ls` command defaults to the current working directory (/home/you).

What happens when Carla types `ssh otheruser@host ls ~`?

1) Your local bash expand ~ to /home/you

2) ssh logs you in as someotheruser and you end up at host:/home/otheruser

3) from that directory the command `ls /home/you` is executed. Three things can happen now:

3.1) /home/you does not exist at the remote host. You get an error.

3.2) otheruser is not allowed to access /home/you on the remote host. You get access denied.

3.3) You get the contents of /home/you
tracyanne

Nov 23, 2008
12:32 AM EDT
quote]carla@host1:~$ ssh terry@host2 ls ~ [/quote]

Carla all you are doing is logging in via ssh as a user on the remote machine.
herzeleid

Nov 23, 2008
1:17 AM EDT
@sander -

All very true. I was speaking of the default case. In practice, I never have occasion to log in via ssh as another normal user. I either log in as myself, or as root. Once logged in as root, I can become any user at will.

I have the same login name on every unix system, so hassle is at a minimum.
Sander_Marechal

Nov 23, 2008
3:44 PM EDT
But back to my original question. Say I did this:

sander@desktop:~$ ssh host1 sander@host1:~$ ssh host2 sander@host2:~$ ssh host3 sander@host3:~$

And now I see a file on host3 that I want to copy to my desktop. How do I do that?
krisum

Nov 23, 2008
4:56 PM EDT
Is host3 not reachable from your desktop and scp from host3 to desktop not work? If host3 is reachable from host1, then you could set up a tunnel from host1 to host3 first and then use scp like:

ssh -L 1234:host3:22 sander@host1 scp -P 1234 sander@localhost:/file-path .

If host3 is only reachable from host2 then you will probably require two tunnels:

ssh -L 1234:host2:22 sander@host1 ssh -L 3456:host3:22 -p 1234 sander@locahost scp -P 3456 sander@localhost:/file-path .

You may want to disable compression the second time in the above case using "-o Compression=no"

The above ssh commands will open normal ssh sessions so you can work in them as usual (in addition to being able to scp directly from your desktop as above).

edit: The comment regarding compression, of course, is valid only if you have turned on compression by default in ssh client configs.
Sander_Marechal

Nov 23, 2008
8:39 PM EDT
No easy way eh?! :-(

I would have thought that someone would have made something that makes this (particularly common) situation easier.
krisum

Nov 23, 2008
11:54 PM EDT
Ya, its hard to see how this can be done without setting up explicit tunnels and the normal ssh commands do not do port forwarding. Maybe if this is common scenario you could have scripts that always do ssh port forwarding for the hosts in question and use them instead of the normal ssh commands.
hkwint

Nov 26, 2008
1:55 PM EDT
Sander: I'd suggest bugging PaulFericyde to make his BatchLogin program which he announced on LXer 'some time' ago support your needs.

More info here: http://batchlogin.sourceforge.net/

Posting in this forum is limited to members of the group: [ForumMods, SITEADMINS, MEMBERS.]

Becoming a member of LXer is easy and free. Join Us!