#!/bin/bash ########################################################################## # # # Pushkey copyright Sam Rowe http://www.deadman.org # # # # Released under a BSD-style license # # # # For usage, simply run pushkey # # # ########################################################################## KEY="$HOME/.ssh/id_dsa.pub" if [ ! -f ${KEY} ] ; then echo "No public key found, you must use -f"; fi if [ $# -eq 0 ] ; then echo "Usage: pushkey -h HOST [-f file] [-u REMOTE_USERNAME] [-c]" echo " -c converts the key to 'SECSH Public Key File Format' (SSH.com's keyformat.)" exit 1 fi while [ $# -ne 0 ] ; do if [ "${1}" = "-h" ] ; then if [ -n "${2}" ] ; then HOST="${2}" else echo "You must supply a host! Aborting." exit 1 fi fi if [ "${1}" = "-c" ] ; then CONVERT="YES" fi if [ "${1}" = "-f" ] ; then if [ -n "${2}" ] ; then if [ -f "${2}" ] ; then KEY="${2}" else echo "No such file. Aborting." exit 1 fi else echo "You must supply an argument to -f! Aborting." exit 1 fi fi if [ "${1}" = "-u" ] ; then if [ -n "${2}" ] ; then USER="${2}" else echo "You must supply an argument to -u! Aborting." exit 1 fi fi shift done if [ -z "${HOST}" ]; then echo "You must supply a host! Aborting." exit 1 fi if [ -n "${CONVERT}" ] ; then MYHOST=`hostname` RF="/tmp/${USER}"`date +"%N"` ssh-keygen -f $KEY -e > $RF if [ $? -ne 0 ] ; then echo "Something went wrong converting your key. Aborting" rm $RF exit 1 fi KEY=$RF ssh ${USER}@${HOST} "cat >> ~/.ssh2/${MYHOST}.pub ; echo \"Key ${MYHOST}.pub\" >> ~/.ssh2/authorization " < $KEY if [ $? -ne 0 ] ; then echo "Something went wrong transferring your key. Aborting" rm $RF exit 1 fi rm $RF exit 0 fi ssh ${USER}@${HOST} 'cat >> ~/.ssh/authorized_keys' < $KEY