I have some arbitrary number of servers with the same user/pass combination. I want to write a script (that I call once) so that
ssh-copy-id user@myserver
is called for each server. Since they all have the same user/pass this should be easy but ssh-copy-id
wants me to type the password in separately each time which defeats the purpose of my script. There is no option for putting in a password, ie ssh-copy-id -p mypassword user@myserver
.
How can I write a script that automatically fills in the password field when ssh-copy-id
asks for it?
Take a look at sshpass. Place your password in a text file and do something like this:
You can use expect to listen for the password prompt and send your password:
Save the script, make it executable, and call it like:
./login.expect user@myserver
quanta's answer is pretty good, but it requires you to put your password in a text file.
From the
sshpass
man page:So, what you can do is to capture the password once during the script, store it in a variable, echo the password and pipe that to sshpass as an input.
I do this all the time and it works fine. Example:
This is a problem with ssh-copy-id; it also adds a key every time you run it. If you are automating the process, your authorized_keys file quickly gets cluttered with duplicate keys. Here is a Python program that avoids both problems. It runs from the control server and puts the keys from one remote server into another remote server.
Rather than type your password multiple times you can make use of
pssh
and its-A
switch to prompt for it once, and then feed the password to all the servers in a list.NOTE: Using this method doesn't allow you to use
ssh-copy-id
, however, so you'll need to roll your own method for appending your SSH pub key file to your remote account's~/.ssh/authorized_keys
file.Example
Here's an example that does the job:
The above script is generally structured like so:
High level
pssh
detailscat <pubkey>
outputs the public key file topssh
pssh
uses the-I
switch to ingest data via STDIN-l <remote user>
is the remote server's account (we're assuming you have the same username across the servers in the IP file)-A
tellspssh
to ask for your password and then reuse it for all the servers that it connects to-i
tellspssh
to send any output to STDOUT rather than store it in files (its default behavior)'...cmds to add pubkey...'
- this is the trickiest part of what's going on, so I'll break this down by itself (see below)Commands being run on remote servers
These are the commands that
In order:pssh
will run on each server:set the remote user's umask to 077, this is so that any directories or files we're going to create, will have their permissions set accordingly like so:
create the directory
~/.ssh
and ignore warning us if it's already there$afile
, with the path to authorized_keys filecat - >> $afile
- take input from STDIN and append to authorized_keys filesort -u $afile -o $afile
- uniquely sorts authorized_keys file and saves itNOTE: That last bit is to handle the case where you run the above multiple times against the same servers. This will eliminate your pubkey from getting appended multiple times.
Notice the single ticks!
Also pay special attention to the fact that all these commands are nested inside of single quotes. That's important, since we don't want
$afile
to get evaluated until after it's executing on the remote server.I've expanded the above so it's easier to read here, but I generally run it all on a single line like so:
Bonus material
By using
pssh
you can forgo having to construct files and either provide dynamic content using-h <(...some command...)
or you can create a list of IPs using another ofpssh
's switches,-H "ip1 ip2 ip3"
.For example:
The above could be used to extract a list of IPs from my
~/.ssh/config
file. You can of course also useprintf
to generate dynamic content too:For example:
You can also use
seq
to generate formatted numbers sequences too!References & similar tools to
pssh
If you don't want to use
pssh
as I've done so above there are some other options available.One of the parallel SSH tools (clusterssh, mssh, pssh) may be appropriate for you.
For instance, use cssh to log into all the machines and append the key yourself.
Couple of things that might fit the bill:
As mentioned in other answers, sshpass is likely the easiest solution.
I want to stress just how bad of an idea it is to:
Here is an implementation that is a bit more secure...