OK this is my first real attempt at a BASH script and I am looking for a little help. Basically, it's a small script that adds my SSH key to remote servers. For the most part it works but I had a couple of bugs I was trying to fix. The "working" version can be downloaded here
http://esolves.googlepages.com/ssh-keyadd
The main bug I am trying to fix is if the directory or file on the remote server does not exist, I want the script to create it. Problem is, the user can pass the paths. If the user passes a path with a ~ in it (which is the default), I can't figure out how to tell the BASH script to break down the path and figure out the directory and file with out it processing the ~ on the local machine.
So if the user passes a location of ~/home/remote/dir/.ssh/authorized_keys. I need the script to figure out the directory path is ~/home/remote/dir/.ssh/ and the file is authorized_keys. I tried using the dirname command to define the directory and it works except in the case of a ~ like in the example. In that case it processes the path on the local machine and defines it there.
Does anyone have any ideas?
Here is the current code I am working with
Code:
#!/bin/sh
# ssh-keyadd Script V.1
# Author Eric Van Johnson (C)2006
# License: New BSD License
#
# You may freely distribute this script as long as all comments
# remain in this file.
#
# WARNING - This is my first real bash script I've ever written.
# I am sure if you tried you could figure out a better way of
# doing this :-)
# With that said, hope you do find this script helpful
#
# Usage:
# ssh-keyadd [-h this help] [-l username] [-p port]
# [-k key file] [-a remote authorized_keys] remote_server
# Define the usage
usage="Usage: ssh-keyadd [-h this help] [-l username] [-p port] [-k key file] [-a remote authorized_keys] remote_server..."
# Check for Options and Arguments
while getopts ":l:p:k:a:h:" opt; do
case $opt in
l ) user=$OPTARG;;
p ) if [ $(echo $OPTARG | grep '^[0-9]*$') ];then
port=$OPTARG
else
echo "Port needs to be set to a number"
echo $usage
exit
fi;;
k ) key=$OPTARG;;
a ) auth=$OPTARG;;
h ) echo -e $usage;;
\? ) echo -e $usage
exit;
esac
done
shift $((OPTIND -1))
if [ -z "$@" ]; then
echo $usage
exit 1
fi
# Define the variables
user=${user:-`whoami`} # If a user name isn't passed I'll assume I can use your current username
port=${port:-22} # Unless I was told differently, I will use default SSH port
key=${key:-"$HOME/.ssh/id_rsa.pub"} # Location of local public key you want to push
auth=${auth:-"~/.ssh/authorized_keys"} # Authorized file location on remote server
authdir=`dirname $auth` # Authorized directory on remote server
key2=`cat $key`
remote=${1:?"You need to provide a remote server."}
if [ $(echo $remote | grep '^[0-9a-zA-Z.-_]*$') ];then
set=1
else
echo "Remote server needs to be an IP address of URI"
echo $usage
exit
fi
# Update remote server with key. This should be the last time
# you are prompted for a password
echo "Adding the following key -> $key "
echo "To the following remote server -> $remote "
echo "For the following user -> $user "
echo "If this is all correct, please enter password for the remote server"
# Now actually do some work
ssh -l $user -p $port $remote "mkdir -p -m 0700 $authdir | echo $key2 >> $auth | chmod 700 $auth"
# If Key updates successfully we should be able to
# SSH to the location. Let's try
echo "------------KEY ADDED--------------"
echo "The key has been added to the remote server, now lets try and connect"
#ssh -l $user -p $port $remote