Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

First, I'm TRYING to use rsa keys because I'm using the script to create RSA keys, but initially I have to connect to the remote machine to pass over the newly generated authentication key. Having premade keys is what I'm doing, but I'd like an easier way to get the key to all of these machines.

So here's the deal: I need to connect to 100+ machines and pass over a public key file plus set permissions and turn on key authentication. Right now I can do it in 2 lines: one scp command to transfer and one ssh command to set permissions and turn key authentication back on. But as it is, it's asking for the password twice, for each machine. With over 100 machines needing these keys so I never have to do the password thing again, I don't want to type this password 200 times.

I need some way to insert the password during the connection, just this once (well twice technically). I was hoping for the ability to store the password in a variable while the script ran and then be done with it after the connections are made and the public key is transferred to all these machines.

Thoughts?

Edit: forgot to mention, I can't install anything. So I can't use expect.

share|improve this question
3  
man 1 ssh-copy-id ... a command which is much less well known than it should be. –  derobert Nov 2 '12 at 17:32
    
You can't install anything on your local machine? You'd only want expect there, not on all the remote machines. –  derobert Nov 2 '12 at 17:39
    
It's complicated. –  Kraden Nov 2 '12 at 18:20
add comment

1 Answer 1

You don't need to do two connections to do what you're doing. Assuming ssh-copy-id won't work for you, here is something from a script I wrote a long time ago (before I knew about ssh-copy-id) that does it in one ssh:

tar c $keys | ssh "$1" "set -e; cd \`mktemp -d\`; tar x; mkdir -p ~/.ssh; chmod go-w ~/.ssh; cat $keys >> ~/.ssh/authorized_keys; chmod go-w ~/.ssh/authorized_keys"

Yes, that's one huge line. $keys holds the names of all the public keys (remember, this is an old script):

keys=''
for k in identity.pub id_rsa.pub id_dsa.pub; do
        [ -e ~/.ssh/$k ] && keys="$keys $k"
done

The key thing is ssh is a binary-safe transport, so you can use it to transfer tar to get your files across in the same session as is running your chmod, etc.

share|improve this answer
    
I hadn't thought of putting the content of the key into memory...I'll try this out. –  Kraden Nov 2 '12 at 18:20
    
I didn't try this exact thing, but the idea was sound and is now working. ssh can echo a variable into a file on the remote server and in essence "transfer" that file. Really you're just creating a new file on the remote server that contains the same information. NOTE: I am still prompted for the password, but instead of it being multiple times, it's just the once, which is what I wanted. Thanks again! –  Kraden Nov 2 '12 at 22:21
add comment

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.