Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to copy some data from a cluster of very restricted machines through the network which currently has DNS problems (so some connections are bizarrely not working). I found that the only way to copy the data from a machine is running scp from that machine. For now the workflow is:

  1. for each machine in machines
  2. ssh machine
  3. enter password
  4. scp files destination:results
  5. enter password
  6. wait
  7. repeat forever

I'm going mad over this manual labor. I want a solution which allows me to enter the password a single time. I've searched for solutions. Most recommend installing SSH keys, but this requires the same procedure to distribute the keys. Other mention the expect program, but I don't have it on that system.

How can this be accomplished?

Update: The main machine runs FreeBSD 9 and the 100 servers run Ubuntu 12. I don't have root on any machine.

share|improve this question
    
Is there a different password for each machine? –  terdon Apr 7 '13 at 1:52
    
The username and password are the same. –  Ark-kun Apr 7 '13 at 1:55
    
What OS are you using? If linux, which distribution? –  terdon Apr 7 '13 at 2:01
    
@terdon The main machine runs FreeBSD 9 and the 100 servers run Ubuntu 12. P.S. I don't have root on any machine. –  Ark-kun Apr 25 '13 at 14:45
    
Make a habit of distributing your SSH public keys as soon as you receive access to any server through your life and you'll thank yourself forever. That said, when I receive access to large batches of servers at once I run: for s in $LIST_OF_SERVERS; do ssh-copy-id $s; done and put my password (plus newline) in the clipboard. Manual labor still, but goes by quiker, and, again will only need to be done once per server. –  Ekevoo Aug 21 at 20:11

1 Answer 1

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

sshpassis 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
share|improve this answer
    
Sorry, haven't noticed your answer. My problem is that I didn't have root on any machine. The main machine had FreeBSD 9 and the 100 servers had Ubuntu 12. –  Ark-kun Apr 25 '13 at 14:44
    
@Ark-kun that's a problem. You could simplify things by running the scp from your machine, that way at least you won't need to ssh: scp user@ip:/remote/path /local/path. Then, if you copy your password to the clipboard you can just middle click 100 times to enter the password. –  terdon Apr 25 '13 at 14:51
    
At that time I couldn't run scp from the main machine because of some strange DNS-related problems. It only worked the other way. Thatnks for the suggestion. Does sshpass really require root? –  Ark-kun Apr 25 '13 at 14:58
    
@Ark-kun no, only to install it the first time. –  terdon Apr 25 '13 at 15:14
    
I meant "Can your proposed solution be used without having root anywhere?" What if I build sshpass from source and skip the package manager? I guess no. –  Ark-kun Apr 25 '13 at 21:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.