Skip to content

Instantly share code, notes, and snippets.

@decthomas
Created February 7, 2012 21:03

Revisions

  1. Thomas Deceuninck created this gist Feb 7, 2012.
    32 changes: 32 additions & 0 deletions .bash_profile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    # 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