Use at own risk. Programs haven't been thoroughly tested.

script: instkey.bash

A BASH script to install SSH keys remotely, so you never have to login again. This bash script can be used to install SSH keys on remote servers. This can potentially be a security risk, but it can also be very effective if used in load balaced clusters, or trusted networks, because you can automate remote tasks since with SSH Keys, there is no login needed to SCP something to another server. You can also execute remote commands with SSH, or rsync something without logging in.

Info

@author Kevin van Zonneveld
@version 0.92
@link http://kevin.vanzonneveld.net
@param (hosts) string where you want to install your key

Example

Usage
./instkey.bash 192.168.0.2

Outputs
Install my local SSH key at 192.168.0.2 (Y/n) Y
root@192.168.0.2's password:
OK

Source Code

download source
#!/bin/bash
#
 
REMOTE_USER="root"
 
err(){
    echo "ERROR: ${1} Aborting..."
    exit 1
}
 
installkeyat(){
    if [ -n "${1}" ];then
        REMOTE_HOST="${1}"
    else
        err "1st argument should be the remote hostname."
    fi
 
    if [ -n "${2}" ];then
        REMOTE_USER="${2}"
    else
        REMOTE_USER="root"
    fi
 
    [ -d "~/.ssh" ] || mkdir -p ~/.ssh
    if [ ! -f ~/.ssh/id_dsa.pub ];then
        echo "Local SSH key does not exist. Creating..."
        echo "JUST PRESS ENTER WHEN ssh-keygen ASKS FOR A PASSPHRASE!"
        echo ""
        ssh-keygen -t dsa -f ~/.ssh/id_dsa
 
        [ $? -eq 0 ] || err "ssh-keygen returned errors!"
    fi
 
    [ -f ~/.ssh/id_dsa.pub ] || err "unable to create a local SSH key!"
 
 
    while true; do
        echo -n "Install my local SSH key at ${REMOTE_HOST} (Y/n) "
        read yn
        case $yn in
            "y" | "Y" | "" )
                echo "Local SSH key present, installing remotely..."
                cat ~/.ssh/id_dsa.pub | ssh ${REMOTE_USER}@${REMOTE_HOST} "if [ ! -d ~${REMOTE_USER}/.ssh ];then mkdir -p ~${REMOTE_USER}/.ssh ; fi && if [ ! -f ~${REMOTE_USER}/.ssh/authorized_keys2 ];then touch ~${REMOTE_USER}/.ssh/authorized_keys2 ; fi &&  sh -c 'cat - >> ~${REMOTE_USER}/.ssh/authorized_keys2 && chmod 600 ~${REMOTE_USER}/.ssh/authorized_keys2'"
                [ $? -eq 0 ] || err "ssh returned errors!"
             break ;;
            "n" | "N" ) echo -n "" ; break ;;
            * ) echo "unknown response.  Asking again" ;;
        esac
    done
}
 
 
installkeyat ${1} ${2}

Add comment

» Currently away on vacation. I can reply your message the 24th of July 2008. Please post anyway and check back then. Thank you!

for syntax highlighting

[CODE="Javascript"]
your_code_here();
[/CODE]

Replace "Javascript"
with "php", "text", etc.
code (to make sure you are not a spammer)

Comments

#2. Thank you! on 28 March 2008

Thank you!that's what i was looking for. Thank you and keep on..

Best regards,

#1. K2 on 08 September 2007

K2There's also a script called "ssh-copy-id" that's even included in some distributions