0

I want to copy a file via ssh when I've logged in. What scp does is that it copies the files by login, password, path. What I want is to be able to do the same thing when I'm "inside", ideally without providing the login and password:

$ ssh root@123.123.243.63
root's password:

Welcome to .....
Last login: ....
root@folder1:~# // How do I download (or upload) the files when I'm here?
Incerteza
  • 2,671
  • 1
    Assuming you want to copy from ssh client, for medium/small text files files the clipboard can be surprisingly reliable.

    Otherwise, a duplicate question: http://unix.stackexchange.com/questions/106480/how-to-copy-files-from-one-machine-to-another-using-ssh and http://unix.stackexchange.com/questions/4552/copy-text-from-one-terminal-into-another/4557#4557

    – Ian McGowan Dec 09 '14 at 04:39
  • If your remote machine can access the local SSH server, you can always scp the other way around. scp host:/path/to/file . (related: http://askubuntu.com/questions/13382/download-a-file-over-an-active-ssh-session) – John WH Smith Dec 09 '14 at 08:21

2 Answers2

1

As pointed out in this Ask Ubuntu answer, you may use zssh instead of ssh. When using zssh, you'll be able to switch to file transfer mode using Ctrl + @. This should allow you to transfer files back and forth between the local and remote ends of your active connection.

Another trick pointed out in that answer is to send ssh to the background while you call scp. This may require re-authentication though, but if you setup passkey authentication, this shouldn't be a problem. To send ssh to the background, hit Ctrl + Z. You should then be able to call scp, and get ssh back when you're done, using fg.

Edit: as pointed out by muru, in order to handle Ctrl + Z locally, and not remotely, you'll have to use Enter, ~, Ctrl + Z. See about SSH escape sequences.

Basically, scp and ssh just don't work the same way. You'll need a trick to make one do the other's job. Some of those tricks are given in the question I linked earlier.

John WH Smith
  • 15,880
  • Is sending ssh to the background shell dependent? I couldn't do it on zsh with Ctrl-Z. – muru Dec 09 '14 at 08:33
  • @muru This is handled by the terminal, but requires a little bit more setup (see the Ask Ubuntu answer) ; otherwise, the Ctrl+Z sequence is handled remotely. I'll edit my answer. – John WH Smith Dec 09 '14 at 08:35
  • Ah, so this is through Enter-~. – muru Dec 09 '14 at 08:36
0

If you set up a control file, you can reuse the existing connection authentication. From man ssh_config:

 ControlMaster
         Enables the sharing of multiple sessions over a single network
         connection.  When set to “yes”, ssh(1) will listen for
         connections on a control socket specified using the ControlPath
         argument.  Additional sessions can connect to this socket using
         the same ControlPath with ControlMaster set to “no” (the
         default). ...
         Two additional options allow for opportunistic multiplexing: try
         to use a master connection but fall back to creating a new one if
         one does not already exist.  These options are: “auto” and
         “autoask”.  The latter requires confirmation like the “ask”
         option.

So, if you connect using the options -o ControlMaster=auto -o ControlPath=/tmp/ssh-%h-%r, the first connection can be reused for scp. This can be set in your ~/.ssh/config:

Host some-host
  ControlMaster auto
  ControlPath=/tmp/ssh-%h-%r
muru
  • 72,889