Skip to content

Instantly share code, notes, and snippets.

@decthomas
Created February 7, 2012 21:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save decthomas/1761938 to your computer and use it in GitHub Desktop.
Save decthomas/1761938 to your computer and use it in GitHub Desktop.
Configure ssh host
# creates an SSH key and uploads it to the given host
configure_ssh_host()
{
username=$1
hostname=$2
identifier=$3
keyfile=$4
if [[ "$identifier" == "" ]] || [[ "$username" == "" ]] || [[ "$hostname" == "" ]] || [[ "$keyfile" == "" ]]
then
echo "usage: configure_ssh_host <username> <hostname> <identifier> <keyfile>"
else
ssh-keygen -f ~/.ssh/$keyfile.id_rsa -C "$USER $(date +'%Y/%m%/%d %H:%M:%S')"
echo -e "Host $identifier\n\tHostName $hostname\n\tUser $username\n\tIdentityFile ~/.ssh/$keyfile.id_rsa" >> ~/.ssh/config
ssh $identifier 'mkdir -p .ssh && cat >> ~/.ssh/authorized_keys' < ~/.ssh/$keyfile.id_rsa.pub
tput bold; ssh -o PasswordAuthentication=no $identifier true && { tput setaf 2; echo 'Success!'; } || { 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
}
# adds ~/.ssh/config to the ssh autocomplete
ssh_load_autocomplete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment