Addremote - Noah.org

Addremote

From Noah.org

Jump to: navigation, search


This is obsolete. Use `ssh-copy-id` which comes with openssh.

This script copies an SSH public key to a remote server. This is very handy for copying keys to remote servers. This will ask for your remote host password twice.

Download addremote

#!/bin/sh
# This creates; copies; and installs a public key on a remote server.
# $Id: addremote 170 2008-01-15 11:43:33Z root $
# Noah Spurrier
 
usage() {
    echo
    echo "usage: addremote [-p PORT] [-e] [-v] remote_hostname"
    echo "-p PORT : use the given PORT for ssh and scp instead of default port 22."
    echo "-e      : encrypt rsa key generated by ssh-keygen. This is what you"
    echo "          normally use for managing your connections with ssh-agent. "
    echo "          Do not use this if you are setting up password-less connections"
    echo "          for automated accounts."
    echo "-v      : verbose output."
}
 
PORT=22
while getopts ":p:evh" options; do
    case $options in
        p ) PORT=$OPTARG;;
        e ) ENCRYPT=1;;
        v ) VERBOSE=1;;
        h ) usage
            exit 1;;
        \? ) usage
            exit 1;;
        * ) usage
            exit 1;;
    esac
done
shift $(($OPTIND - 1))
REMOTE=$1
RSA_PRIV=~/.ssh/id_rsa
if [ -z $REMOTE ]; then
    echo "Missing argument. Give username and remote host name in this format:"
    echo "    $0 username@remote.example.com"
    usage
    exit 1
fi
if [ -r $RSA_PRIV ]; then
    echo "Using existing key: $RSA_PRIV"
else
    if [ $ENCRYPT ]; then
        if [ $VERBOSE ]; then
            echo ssh-keygen -t rsa -f $RSA_PRIV
        fi
        ssh-keygen -t rsa -f $RSA_PRIV
    else
        echo "WARNING! Creating new, unprotected key: $RSA_PRIV"
        if [ $VERBOSE ]; then
            echo ssh-keygen -q -t rsa -N '' -f $RSA_PRIV
        fi
        ssh-keygen -q -t rsa -N '' -f $RSA_PRIV
    fi
fi
# Test if the private key is encrypted.
ssh-keygen -q -y -P '' -f $RSA_PRIV > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "WARNING! Private key is not password protected."
    echo "This is not secure if you don't know what you are doing."
fi
echo "Copying public key to remote host," $REMOTE
if [ $VERBOSE ]; then
    echo scp -P $PORT $RSA_PRIV.pub $REMOTE:~/id_rsa.pub
fi
scp -P $PORT $RSA_PRIV.pub $REMOTE:~/id_rsa.pub
echo "Adding public key to authorized_keys on remote host," $REMOTE
if [ $VERBOSE ]; then
    echo ssh -p $PORT $REMOTE "mkdir -p ~/.ssh;chmod 700 ~/.ssh;touch ~/.ssh/authorized_keys;cat ~/id_rsa.pub >> ~/.ssh/authorized_keys"
fi
ssh -p $PORT $REMOTE "mkdir -p ~/.ssh;chmod 700 ~/.ssh;touch ~/.ssh/authorized_keys;cat ~/id_rsa.pub >> ~/.ssh/authorized_keys"
-->