ssh-lastpass/ssh.sh

45 lines
979 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
2019-03-19 11:06:07 -04:00
keyname="${1}"
2019-02-07 13:43:22 -05:00
2019-03-19 09:01:05 -04:00
username=${2}
hostname=${3}
2019-02-07 13:43:22 -05:00
2019-03-19 11:06:07 -04:00
keysavename=$(echo -e ${keyname} | tr -d '[:space:]' | awk -F"/" '{print $NF}')
keyfile=$HOME/.ssh/${keysavename}-$(date "+%s")
2019-02-07 13:43:22 -05:00
#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-03-19 11:06:07 -04:00
#Fetch private key and place in /home/$user/.ssh/$keysavename_datetime
lpass show "$keyname" --field="Private Key" > $keyfile
2019-02-07 13:43:22 -05:00
chmod 0600 $keyfile
#store passphrase in a variable
2019-03-19 11:06:07 -04:00
sshpassphrase=$(lpass show "$keyname" --field=Passphrase)
2019-02-07 13:43:22 -05:00
#Add the key to ssh-agent
expect << EOF
2019-02-07 14:06:12 -05:00
spawn ssh-add -t 30 $keyfile
2019-02-07 13:43:22 -05:00
expect "Enter passphrase"
send "$sshpassphrase\r"
expect eof
EOF
#SSH to the host
2019-03-19 09:01:05 -04:00
ssh $username@$hostname
2019-02-07 13:43:22 -05:00
2019-02-07 14:06:12 -05:00
#Remove keyfile
2019-02-07 13:43:22 -05:00
rm -f $keyfile