Bash: tweak and rename ssh-add-host alias.

This commit is contained in:
Artem Sapegin 2013-02-11 00:14:12 +04:00
parent 4d46755ae2
commit b9a475fc4f

View File

@ -111,14 +111,15 @@ ssh-key() {
# Create an SSH key and uploads it to the given host # Create an SSH key and uploads it to the given host
# Based on https://gist.github.com/1761938 # Based on https://gist.github.com/1761938
add-ssh-host() { ssh-add-host() {
username=$1 username=$1
hostname=$2 hostname=$2
identifier=$3 identifier=$3
if [[ "$identifier" == "" ]] || [[ "$username" == "" ]] || [[ "$hostname" == "" ]]; then if [[ "$identifier" == "" ]] || [[ "$username" == "" ]] || [[ "$hostname" == "" ]]; then
echo "Usage: configure_ssh_host <username> <hostname> <identifier>" echo "Usage: ssh-add-host <username> <hostname> <identifier>"
else else
header "Generating key..."
if [ ! -f "$HOME/.ssh/$identifier.id_rsa" ]; then if [ ! -f "$HOME/.ssh/$identifier.id_rsa" ]; then
ssh-keygen -f ~/.ssh/$identifier.id_rsa -C "$USER $(date +'%Y/%m%/%d %H:%M:%S')" ssh-keygen -f ~/.ssh/$identifier.id_rsa -C "$USER $(date +'%Y/%m%/%d %H:%M:%S')"
fi fi
@ -127,6 +128,7 @@ add-ssh-host() {
echo -e "Host $identifier\n\tHostName $hostname\n\tUser $username\n\tIdentityFile ~/.ssh/$identifier.id_rsa" >> ~/.ssh/config echo -e "Host $identifier\n\tHostName $hostname\n\tUser $username\n\tIdentityFile ~/.ssh/$identifier.id_rsa" >> ~/.ssh/config
fi fi
header "Uploading key..."
ssh $identifier 'mkdir -p .ssh && cat >> ~/.ssh/authorized_keys' < ~/.ssh/$identifier.id_rsa.pub ssh $identifier 'mkdir -p .ssh && cat >> ~/.ssh/authorized_keys' < ~/.ssh/$identifier.id_rsa.pub
tput bold; ssh -o PasswordAuthentication=no $identifier true && { tput setaf 2; echo "SSH key added."; } || { tput setaf 1; echo "Failure"; }; tput sgr0 tput bold; ssh -o PasswordAuthentication=no $identifier true && { tput setaf 2; echo "SSH key added."; } || { tput setaf 1; echo "Failure"; }; tput sgr0