ssh-lastpass/ssh.sh
2019-03-19 11:06:07 -04:00

45 lines
979 B
Bash
Executable File

#!/bin/bash
#Maintained by Linux Operations - Russ Long - <rlong@nabancard.com>
#Set variables
keyname="${1}"
username=${2}
hostname=${3}
keysavename=$(echo -e ${keyname} | tr -d '[:space:]' | awk -F"/" '{print $NF}')
keyfile=$HOME/.ssh/${keysavename}-$(date "+%s")
#Check to see if user is logged in to lastpass cli
#check_login()
#{
if lpass status | grep -q "Logged in as"; then
echo "Logged in to Lastpass, continuing..."
else
echo "Please login with 'lpass login email@address.com'"
exit 1
fi
#}
#Fetch private key and place in /home/$user/.ssh/$keysavename_datetime
lpass show "$keyname" --field="Private Key" > $keyfile
chmod 0600 $keyfile
#store passphrase in a variable
sshpassphrase=$(lpass show "$keyname" --field=Passphrase)
#Add the key to ssh-agent
expect << EOF
spawn ssh-add -t 30 $keyfile
expect "Enter passphrase"
send "$sshpassphrase\r"
expect eof
EOF
#SSH to the host
ssh $username@$hostname
#Remove keyfile
rm -f $keyfile