Skip to content

Instantly share code, notes, and snippets.

@samsargent
Last active June 15, 2017 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsargent/eabe56847faf3170e329fbe97e1e0ee6 to your computer and use it in GitHub Desktop.
Save samsargent/eabe56847faf3170e329fbe97e1e0ee6 to your computer and use it in GitHub Desktop.
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