Help - Search - Members - Calendar
Full Version: SSH and BASH
linux-noob.com/forums > Linux Noob > Tips and Tricks
znx
How many times have I seen people using SSH aliases to shorthand the long statements they need to type to login:
CODE
alias ssh-home="ssh -p 12345 znx@somelong.hostname.com"


Now whilst this eases the pain, you are missing some real magic that SSH and BASH can provide you! So lets get started:

Edit/Make the ~/.ssh/config
CODE
Host some
   HostKeyAlias somelong.hostname.com
   HostName somelong.hostname.com
   User znx
   Port 12345


Now what's that all about you say, well now the original long top line can be replaced with:
CODE
ssh some


MAGIC!

But that's not all, you can infact setup BASH to provide you with a tab completion on those new short hostnames!

Edit/Add to your ~/.bashrc
CODE
HOSTFILE=~/.hosts

function _ssh() {
  local cur
  cur=${COMP_WORDS[COMP_CWORD]}
  if [ "${cur:0:1}" != "-" ]; then
    COMPREPLY=( $(awk '/^Host '$2'/{print $2}' $HOME/.ssh/config) )
  fi        
  return 0
}

complete -F _ssh ssh sftp scp
complete -A hostname ssh sftp scp


Edit/Add to a ~/.hosts
CODE
192.168.1.2  some.long.host hostname
192.168.1.3 other.long.host host


To update the current shell you are running do:
CODE
source ~/.bashrc


Now here is the magic:
CODE
ssh s<TAB BUTTON>


At which point you will be given the option of "some" or "some.long.host". Better than all that is this, when you edit your ~/.ssh/config or your ~/.hosts to update or add a new host, it is INSTANTLY in your tab complete.

Weeeeee!

CODE
ssh home
ssh work
ssh somefriend
ssh here
ssh there


wink.gif Love Linux wink.gif
xDamox
Nice tip/trick biggrin.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.