bash ssh autocompletion

1. unix box:

modify:
# add to ~/.bash_profile, and close/reopen a shell. Will autocomplete any hosts found in $HOME/.ssh/known_hosts
complete -W “$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ‘ ‘ | sed -e s/,.*//g | uniq | grep -v “\[“`;)” ssh

result:
sg212148@fsehlp03 ~
$ssh tkthl
tkthlc600 tkthlc601 …

2. cygwin
$ cat .ssh/config
Host int
hostname tkthli700
user sg212148

Host fsehlp*
user sg212148

~/.bashrc:


###############
# My Settings #
###############

# cygwin startup directory
CYGWINDIR=c:/dev/
[[ ! -z `echo $CYGWINDIR | grep -v " "` ]] && cd $CYGWINDIR

# ssh and scp autocompletion
_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
complete -F _complete_ssh_hosts scp


export MAVEN_OPTS=" -Xms1024m -Xmx2048m -XX:MaxPermSize=1024m"
export JAVA_HOME=/cygdrive/c/dev/env/jdk1.7.0_76
export JAVA_HOME=/cygdrive/c/dev/env/jdk1.8.0_31
export M2_HOME=/cygdrive/c/dev/env/apache-maven-3.2.5
export PATH=$JAVA_HOME/bin:$M2_HOME/bin:$PATH

alias psjava='ps -W | grep java'
alias psssh='ps -W | grep ssh'
alias killjava='ps -W | grep java$ | cut -c-9 | xargs /bin/kill -f'
alias killssh='ps -W | grep ssh$ | cut -c-9 | xargs /bin/kill -f'
alias java7='JAVA_HOME="/cygdrive/c/dev/env/jdk1.7.0_76" && PATH=$JAVA_HOME/bin:$PATH'
alias java8='JAVA_HOME="/cygdrive/c/dev/env/jdk1.8.0_31" && PATH=$JAVA_HOME/bin:$PATH'

alias cddev="cd '/cygdrive/c/dev'"
alias cddesktop='cd /cygdrive/c/Users/me/Desktop'
alias cdrepo="cd /cygdrive/c/Users/me/.m2/repository"

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s