Skip to content

Instantly share code, notes, and snippets.

@samsargent samsargent/.bash_profile
Last active Jun 15, 2017

Embed
What would you like to do?
SSH Shortcuts & Helpers
# add this to your .bash_profile
export PATH=$PATH:$HOME/bin
_complete_ssh_hosts ()
{
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
cut -f 1 -d ' ' | \
sed -e s/,.*//g | \
grep -v ^# | \
uniq | \
grep -v "\[" ;
cat ~/.ssh/config | \
grep "^Host " | \
awk '{print $2}'
`
COMPREPLY=( $(compgen -W "${comp_ssh_hosts}" -- $cur))
return 0
}
complete -F _complete_ssh_hosts ssh
#!/bin/bash -e
if [ $# -eq 0 ]
then
echo "Usage: sshutup %user@hostname.com.au%"
exit 1
fi
user=${1%@*}
host=${1#*@}
IP=$(curl -Sfs https://wtfismyip.com/text)
KEY=$(cat ~/.ssh/id_rsa.pub)
echo 'Adding Key to '$host': from="'$IP'"' $KEY
echo 'from="'$IP'"' $KEY | ssh $user'@'$host 'cat >> ~/.ssh/authorized_keys'
ssh $user'@'$host 'chmod 600 ~/.ssh/authorized_keys'
echo "I'll create an ssh shortcut for you, what would you like to call it? eg. clientname"
read shortcut
cat >> ~/.ssh/config << EOL
Host ${shortcut}
HostName ${host}
User ${user}
EOL
echo 'Done, now just use ssh '$shortcut
exit;
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.