www.linux-noob.com: SSH and BASH - www.linux-noob.com

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

SSH and BASH Rate Topic: -----

#1 User is offline   znx

  • Linux-Noob GURU
  • PipPipPipPipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 1,236
  • Joined: 21-March 05

Post icon  Posted 22 March 2007 - 11:37 PM

How many times have I seen people using SSH aliases to shorthand the long statements they need to type to login:
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
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:
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
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
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:
source ~/.bashrc


Now here is the magic:
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!

ssh home
ssh work
ssh somefriend
ssh here
ssh there


;) Love Linux ;)
0

#2 User is offline   xDamox

  • Linux-Noob Frequent Member
  • PipPipPip
  • Group: Members
  • Posts: 390
  • Joined: 13-December 04

Posted 23 March 2007 - 09:13 AM

Nice tip/trick :D
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users





The G2-style Tux and the header images using them are licensed under Creative Commons BY-NC-SA. The G2-style Tux images are all from http://crystalxp.net.
Thanks to users kami23, lilitux, iva, overlord59, whidou, brightknight, emulienfou on the Crystal XP Tux Factory site.