From b237e15983a59fdc49a996bf279ccd07e5434b71 Mon Sep 17 00:00:00 2001 From: Artem Sapegin Date: Fri, 9 Nov 2012 14:05:30 +0400 Subject: [PATCH] Bash: alias for adding ssh hosts and ssh autocomplete. --- includes/bash_functions.bash | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/includes/bash_functions.bash b/includes/bash_functions.bash index d935777..6b7d46e 100644 --- a/includes/bash_functions.bash +++ b/includes/bash_functions.bash @@ -80,3 +80,35 @@ function nyan() { echo -e $RESET$BOLD'"" ""'$RESET echo } + +# Creates an SSH key and uploads it to the given host +configure_ssh_host() { + username=$1 + hostname=$2 + identifier=$3 + + if [[ "$identifier" == "" ]] || [[ "$username" == "" ]] || [[ "$hostname" == "" ]] + then + echo "Usage: configure_ssh_host " + else + if [ ! -f "$HOME/.ssh/$identifier.id_rsa" ]; then + ssh-keygen -f ~/.ssh/$identifier.id_rsa -C "$USER $(date +'%Y/%m%/%d %H:%M:%S')" + fi + + if ! grep -Fxiq "host $identifier" "$HOME/.ssh/config"; then + echo -e "Host $identifier\n\tHostName $hostname\n\tUser $username\n\tIdentityFile ~/.ssh/$identifier.id_rsa" >> ~/.ssh/config + fi + + 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 + + ssh_load_autocomplete + fi +} + +# Adds ~/.ssh/config to the ssh autocomplete +ssh_load_autocomplete() { + complete -W "$(awk '/^\s*Host\s*/ { sub(/^\s*Host /, ""); print; }' ~/.ssh/config)" ssh +} +ssh_load_autocomplete \ No newline at end of file