How can I download a file from a SSH session (without using sftp)

2

I know I can use sftp user@host ....

But what I want is to download a file from the existing session.

that is.

ssh user@host ... do some work ...

The main problem I have is that said file is under root on the server so doing a sftp command is not that simple.

jorge.vargas

Posted 2010-04-15T21:21:18.890

Reputation: 121

2SSH doesn't provide a download facility that you can use within an interactive session, excepting screen captures, but you can use scp as described below. the "main problem" you describe is very vague; please clarify what you're trying to do and what errors you get. exact commands would help. – quack quixote – 2010-04-16T01:28:23.700

Answers

4

The way you describe it it sounds like your issue is that you cannot login as root via ssh (probably you used some sort of su or sudo to work as root).

  • If root can in principle log in via ssh, but you do not know the password, you can set up root's account to allow public-key authentication with your key.
  • If that is not possible copy that file to a folder accessible to the account you log in with (like your $HOME folder), possibly adjust permissions and copy as user with sftp or scp.

Benjamin Bannier

Posted 2010-04-15T21:21:18.890

Reputation: 13 999

Also, if you're logged in and can somehow see that file remotely, can't you just copy it to /tmp temporarily, then scp (or sftp) it down? – Michael H. – 2010-04-15T21:49:13.077

@khedron depending on the file /tmp might be a little bit too visible, but that's hard to tell from the question – Benjamin Bannier – 2010-04-15T21:49:51.453

1

Have you tried scp? For example:

scp user@host:/path/to/remote-file .

akid

Posted 2010-04-15T21:21:18.890

Reputation: 720

1

I don't know why it has to be the same session, but if you use ControlMaster and ControlPath in your ~/.ssh/config you don't have to authorize your scp connection since it multiplexes in the same connection. This also speeds up your connection you do to the same host.

Have a look at http://www.linux.com/archive/feed/54498 on how to set it up.

Jimmy Hedman

Posted 2010-04-15T21:21:18.890

Reputation: 886

0

You cannot download "through" an existing session.

scp and sftp work by creating new sessions and talking to an executable on the other end that's running in place of a shell.

The main problem I have is that said file is under root on the server so doing a sftp command is not that simple.

You are logged into a remote server. I presume you can get to this file under your non-root account. So, scp should work. The user your specify under scp can reach everything as if you were logged in.

If you are needing to sudo or su to get to this file on the remote system, the proper thing to do is copy it to your normal user's home directory, chmod so your normal user owns and can read it, then get it with scp, etc. If the file is very large, chmod the file directly temporarily without making a copy.

Another thing you can try - If you are running your own SSH server at home or wherever, and it's reachable from the remote SSH server you're logged into, you can use scp to send it from your remote session to your local system.

LawrenceC

Posted 2010-04-15T21:21:18.890

Reputation: 63 487

0

The way I would do what you're asking for would be to turn on session logging on your ssh client (Ways to do this vary. Depending on the file you might get away with just setting a big scrollback buffer.), and then base64 encode the file and blit it to your terminal. :D

You can then trim the extraneous bits off of either end of the log and base64 decode it back into your original file.

For example:

base64 < file.to.transfer

And then, on the client end:

base64 -d < trimmed.log.file > destination.file

Some client programs have methods built in for doing pretty much the same thing with, say, the XMODEM protocol or similar, but availability of the transmission software on the server side is probably going to be a lot less than for base64, which is included in most of the Linux distros I've used recently. If you have a client that supports it though, it takes care of the chopping and decoding phase automatically, which can be convenient for large files. I don't know that anybody's really used it since the days of telnet BBSs though, so good luck.

Perkins

Posted 2010-04-15T21:21:18.890

Reputation: 111