Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'A shell script to upload SSH keys to remote machines' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A shell script to upload SSH keys to remote machines
Authored by: slacker on Sep 21, '07 10:42:10AM
Nice script. If you ask three Bourne shell scripters to solve a problem, you'll probably get 9 different ways to solve it. I made a couple of corrections to this version: 1) use $KEY variable consistently, 2) set the umask to user rwx only and you don't need chmods anywhere, 3) use $* to pass "-p portnum" arguments for nonstandard ports, 4) mkdir -p argument ignores existing directories, 5) the KEYCODE variable line wasn't necessary, just in-line the backquoted cat command.

#!/bin/sh                                                                                                                                            
                                                                                                                                                     
KEY="$HOME/.ssh/id_dsa.pub"                                                                                                                          
                                                                                                                                                     
if [ ! -f $KEY ];then                                                                                                                                
    echo "private key not found at $KEY"                                                                                                             
    echo "* please create it with \"ssh-keygen -t dsa\" *"                                                                                           
    exit                                                                                                                                             
fi                                                                                                                                                   
                                                                                                                                                     
if [ -z $1 ];then                                                                                                                                    
    echo "Usage: $0 username@host"                                                                                                                       
    exit                                                                                                                                             
fi                                                                                                                                                   
                                                                                                                                                     
echo "Putting your key on $1... "                                                                                                                    
ssh -q $* "umask 0077; mkdir -p ~/.ssh ; echo "`cat $KEY`" >> ~/.ssh/authorized_keys"                                                                
echo "done!"                                                                                                                                         


[ Reply to This | # ]