ssh-lastpass/ssh.sh

44 lines
912 B
Bash
Raw Normal View History

2019-02-07 13:43:22 -05:00
#!/bin/bash
#Maintained by Linux Operations - Russ Long - <rlong@nabancard.com>
#Set variables
keyname=${1}
hostname=${2}
keyfile=$HOME/.ssh/${keyname}_$(date "+%F-%T")
#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
#}
2019-02-07 13:49:53 -05:00
#Fetch private key and place in /home/$user/.ssh/$keyname_datetime
2019-02-07 13:43:22 -05:00
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 $keyfile
expect "Enter passphrase"
send "$sshpassphrase\r"
expect eof
EOF
#SSH to the host
ssh $hostname
2019-02-07 13:53:59 -05:00
#remove the key from the ssh-agent
ssh-add -d $keyfile
2019-02-07 13:43:22 -05:00
#remove the key
rm -f $keyfile