Bash: alias for adding ssh hosts and ssh autocomplete.

This commit is contained in:
Artem Sapegin 2012-11-09 14:05:30 +04:00
parent e4306a032b
commit b237e15983

View File

@ -80,3 +80,35 @@ function nyan() {
echo -e $RESET$BOLD'"" ""'$RESET echo -e $RESET$BOLD'"" ""'$RESET
echo 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 <username> <hostname> <identifier>"
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