Last active
June 12, 2020 03:25
-
-
Save bcatubig/51023a6ce0dfe6c55159966d8896b2c7 to your computer and use it in GitHub Desktop.
Passwordless SSH Function - inject your ssh key into a server without actually typing your password in; lastpass does that for you!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Works with Bash and ZSH | |
# Copy this into your ~/.bashrc or ~/.zshrc | |
# Install Lastpass cli -- https://github.com/lastpass/lastpass-cli | |
# Install sshpass | |
# OSX: $ brew install http://git.io/sshpass.rb | |
# Be sure to change <MYLASTPASS-PASSWORD-ID> in the install_keys() function | |
function install_keys (){ | |
echo "INFO: No SSH Key on server. Grabbing password from lastpass" | |
export SSHPASS=$(lpass show <MYLASTPASS-PASSWORD-ID> --password) | |
/usr/local/bin/sshpass -e ssh-copy-id $1 || return 255 | |
unset SSHPASS | |
} | |
function ssh (){ | |
# Requires a server parameter | |
if [[ -z $1 ]]; then | |
echo "You didn't specify a server" | |
return 255 | |
fi | |
# Check if key is installed, exit afterwards if successful to avoid duplicate | |
# ssh commands being executed when you log out of the target ssh server | |
/usr/bin/ssh -qo PasswordAuthentication=no $1 exit | |
if [[ $? != 0 ]]; then | |
install_keys $1 | |
fi | |
# Finally, SSH into the server | |
/usr/bin/ssh -qo PasswordAuthentication=no $1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment