#!/bin/sh # AUTHOR: Matt Simerson (matt at tnpi.net) # # VERSION: # v1.02 - Oct 17, 2012 - corrected a warning message # v1.01 - Jun 24, 2008 - added comments, examples, and docs # - add prompts for user and key files # v1.00 - Sep 24, 2007 # # PURPOSE # This script installs my SSH public key on a remote server # I install this script as ~/.ssh/remote.sh and run it like so # # INSTALL # 1. Install this script in your ~/.ssh directory as agent.sh # # curl -o .ssh/remote.sh http://www.tnpi.net/computing/ssh_remote.sh.txt # chmod 755 .ssh/remote.sh # # 2. run it when you want to install your SSH public key on a remote server # # ~/.ssh/remote.sh remote.server.com # # 3. Send gratitude to author # # Examples: # # Usage: remote.sh [remote.host.com] [public.key_filename] # # ~/.ssh/remote.sh matts.spry.com spry_rsa.pub # Remote username: (matt) # Installing spry_rsa.pub in matts.spry.com:.ssh/authorized_keys # Password: # done! # or # # ~/.ssh/remote.sh # Remote hostname: matts.spry.com # Remote username: (matt) # Installing id_rsa.pub in matts.spry.com:.ssh/authorized_keys ... # Password: # done! # you may want to edit these DEBUG=0 REMOTE_HOST=$1 KEY=$2 # below here is not intended to be user servicable get_input() { _input= while : ; do read _input if [ -x "$_input" ]; then err "INVALID, try again." continue fi break done echo "$_input" } get_username() { _input= echo "Remote username: ($USER) \c" read _input if [ -z "$_input" ]; then REMOTE_USER=$USER else REMOTE_USER="$_input" fi } PROMPT="SSH Public Key filename: \c" ERROR="You must enter a file name!" KEY=get_input get_keyfile() { # if a key wasn't specifically entered or doesn't exist, try some defaults if [ ! -f "$HOME/.ssh/$KEY" ]; then KEY="id_rsa.pub" fi if [ ! -f "$HOME/.ssh/$KEY" ]; then KEY="id_dsa.pub" fi if [ ! -f "$HOME/.ssh/$KEY" ]; then echo "SSH Public Key filename: \c" KEY=`get_input` fi if [ ! -f "$HOME/.ssh/$KEY" ]; then echo "ERR: public key not found" echo " * please create it with \"ssh-keygen\" *" exit fi } get_keyfile if [ -z $REMOTE_HOST ]; then echo "Remote hostname: \c" REMOTE_HOST=`get_input` fi get_username REMOTE_CMD='umask 0077; test -d .ssh || mkdir -p ~/.ssh; umask 0022; cat - >> .ssh/authorized_keys' echo "Installing $KEY in $REMOTE_HOST:.ssh/authorized_keys" if [ $DEBUG -ne 0 ]; then echo "cat $HOME/.ssh/$KEY | ssh $REMOTE_USER@$REMOTE_HOST $REMOTE_CMD" fi cat $HOME/.ssh/$KEY | ssh $REMOTE_USER@$REMOTE_HOST $REMOTE_CMD echo "done!"