Skip to content

Instantly share code, notes, and snippets.

@lttlrck
Last active January 4, 2019 18:53
Show Gist options
  • Save lttlrck/95d3952ebf25e36e4356d60118c6a706 to your computer and use it in GitHub Desktop.
Save lttlrck/95d3952ebf25e36e4356d60118c6a706 to your computer and use it in GitHub Desktop.
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