Skip to content

Instantly share code, notes, and snippets.

@lttlrck

lttlrck/bash.sh

Last active Jan 4, 2019
Embed
What would you like to do?
quick SSH using peco, remembers past connections, presents a filterable list. relies on SSH keys for auth.
sss() {
cat ~/.connections | sort | uniq > ~/.connections_ && mv ~/.connections_ ~/.connections
IFS=$'\n' array=($(cat ~/.connections | cut -d" " -f1 | peco --print-query))
r1=${array[1]}
r2=${array[2]}
addr=""
if [[ "$r2" == "" ]]; then
#echo No match, using $r1
addr=$r1
else
# echo match, using $r2
addr=$r2
fi
user=$(cat ~/.connections | grep $addr | cut -d" " -f2)
if [[ "$user" == "" ]]; then
echo Enter user
read user
fi
ssh -o PreferredAuthentications=publickey $user@$addr
if [[ $? == 0 ]]; then
echo $addr $user >> ~/.connections
else
echo Failed, try installing public key
ssh-copy-id $user@$addr
ssh -o PreferredAuthentications=publickey $user@$addr
if [[ $? == 0 ]]; then
echo $addr $user >> ~/.connections
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.