This would be a lot easier with SFTP, which is an extension to SSH that supports more complex file operations than SCP. Virtually all modern Unix and Linux distributions support it. To use it, just run this command to connect to the server:
sftp server
Then you can use the ls
and cd
commands to browse around and find the file you're looking for. Once you've found it, use the get
command to download it. For instance, to download file.txt
in your current working directory on the server to your current working directory on your local machine, just run:
get file.txt
To download /home/pavpanchekha/textfiles/file.txt
on the server to ~/textfiles/
on your local machine, instead run:
get /home/pavpanchekha/textfiles/file.txt ~/textfiles/
Conversely, you can also upload files in this manner. To upload file.txt
from your local current working directory to the current working directory of the server, type:
put file.txt
You can also use full paths for each like you can with get
:
put ~/textfiles/file.txt /home/pavpanchekha/textfiles/file.txt
For a full list of available SFTP commands, just run help
at the sftp>
prompt.
I will assume that these are all Linux hosts and that you have root access to them. I will also assume that they are using apt
for package management, but it is easy to apply this solution with yum
or pacman
or any other package manager.
As far as I know, ssh
offers no way of specifying a password on the command line so first you will need to install sshpass
on your local machine:
sudo apt-get install sshpass
This will allow you to pass the password as a command line argument:
sshpass -p '<password>' ssh user@server
Now, create a file with all the IPs you are interested in, one per line. You can parse this file to install sshpass
on each remote machine. If your password is foo
and you are connecting as root
, you can do
while read ip; do
sshpass -p 'foo' ssh root@$ip "apt-get install sshpass";
done < ips.txt
sshpass
is now installed on each server. Now, go through the servers again, and copy your files. If you are copying as root
and the root
password for your destination
is bar
, do this:
while read ip; do
sshpass -p 'foo' ssh root@$ip "sshpass -p 'bar' scp files dest:/results/";
done < ips.txt
You can also use sshpass
to copỳ your key files and --as long as you've used an empty passphrase-- allow passwordless access so you don't have this problem next time around:
while read ip; do
sshpass -p 'foo' ssh root@$ip \
"sshpass -p 'bar' ssh-copy-id -i ~/.ssh/id_rsa.pub root@destination";
done < ips.txt
Once that is done, you will be able to copy the files over using:
while read ip; do ssh root@$ip "scp files dest:/results/"; done < ips.txt
Best Answer
You want to set up port forwarding. When you SSH from machine A to machine B add a tunnel, i.e. add -L 1234:A:22 when tunnelling to B. Then SSH to machine C and scp the files to B on port 1234 (i.e. add -P 1234 to scp). This will actually route to port 22 on machine A which is the ssh port and hence it'll transfer directly.
I don't have three machines with which to try this right now but I think this should work.