How to download a file on a server you've already ssh'ed into?

6,751

Solution 1

What you need to answer is why you don't want to log again ... if you logged once, you can log twice ... especially if you've taken the three minutes it takes to create a key and store the key remotely:

$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
2b:4c:11:ab:53:d1:15:90:4f:88:a1:42:da:c5:1c:98 user@localhost
$ ssh-copy-id user@remotehost
Warning: Permanently added 'remotehost' (RSA) to the list of known hosts.
Password:
$ ssh user@remotehost
Last login: Mon Dec 14 21:52:45 2009 from 1.2.3.4
[user@remotehost] $

Back in the times before TCP/IP was commonplace, people used kludges such as X/Modem to handle this. Using this nowadays is kinda silly.

Solution 2

It's a little archaic, but you may be able to use something like kermit to use a modem-era protocol (zmodem, etc.). Looks like there's a program meant just for that purpose, too.

I once needed to download a small-ish file from a remote unix server without any supporting tools, so I uuencoded the file, dumped it with cat to the terminal, and then captured the the resulting text with my local terminal program, where I uudecoded it. Sick, eh? :)

Solution 3

SCP is the right tool for the job. Just initiate the scp from the local machine, so you would use:

scp user@remotehost:/path/to/file/filename ~/

...which would put the file on your local box in your home directory.

Solution 4

If you want a point and click option you could use Konqueror and the fish protocol. Just open Konqueror and in the address bar write fish://user@remote with user being the the user you wish to log in as and remote being the remote computer.

Solution 5

I came up with a way to do this with a standard ssh client. It's a script that duplicates the current ssh connection, finds your working directory on the remote machine and copies back the file you specify to the local machine. It needs 2 very small scripts (1 remote, 1 local) and 2 lines in your ssh config. The steps are as follows:

1) Add these 2 lines to your ~/.ssh/config
ControlMaster auto
ControlPath ~/.ssh/socket-%r@%h:%p

Now if you have an ssh connection to machineX open, you wont need passwords to open another one.

2) Make a 1-line script on the remote machine called ~/.grabCat.sh
#!/bin/bash
cat "$(pwdx $(pgrep -u $(whoami) bash) | grep -o '/.*' | tail -n 1)"/$1

3) Make a script on the local machine called ~/.grab.sh
#!/bin/bash
[ -n "$3" ] && dir="$3" || dir="."
ssh "$1" ".grabCat.sh $2" > "$dir/$2"

4) and make an alias for grab.sh in (~/.bashrc or wherever) alias grab=~/.grab.sh

That's it, all done. From now on if you're logged in to "machineX:/some/directory", just fire up a new terminal and type
grab machineX filename

That puts the file in your current working directory on the local machine. You can specify a different location as a third argument to "grab".

Note: Obviously both scripts must be "executable", ie chmod u+x filename

Share:
6,751

Related videos on Youtube

Wilhelm
Author by

Wilhelm

I'm currently working on: http://www.cubehero.com I cofounded previous startups: http://www.noteleaf.com http://www.mobtropolis.com I worked at a startup and a research lab: - http://www.frogmetrics.com - http://www.jhuapl.edu Follow Me: - http://twitter.com/iamwil See my code: - http://github.com/iamwilhelm What I'm working on: - http://geekli.st/iamwil My CV: - http://careers.stackoverflow.com/iamwil Email Me: - [email protected] You can visit the original "The Web and all that Jazz" at http://webjazz.blogspot.com/

Updated on September 17, 2022

Comments

  • Wilhelm
    Wilhelm over 1 year

    I know how to use scp or wget to download a file on a remote server to my local machine. However, if I'm already logged into a server with ssh, is there a command that lets me download a file in the pwd on the server onto my local machine?

    I suppose I can use scp, but my local machine is usually behind a router. Would I have to open a port in the router?

  • niXar
    niXar over 14 years
    Can't Konqueror use sftp instead? In Gnome you can use sftp:// urls; gvfs will automagically mount the remote host with fuse/sshfs for apps that don't support gvfs directly.
  • Alex
    Alex over 14 years
    I feel a little odd voting this one up...but I had the same thought as a solution to this question :)
  • mjrider
    mjrider almost 13 years
    so how do I log in next time...will it "just work" with out the password when I try to login?
  • niXar
    niXar almost 13 years
    Yes, though it will ask for a passphrase if you set one up — which is optional. However you can use ssh-agent so that you only have to enter it once per session.