Last active
January 7, 2019 08:33
-
-
Save Hiteshm01/afbfcc4abc464e7a3e72 to your computer and use it in GitHub Desktop.
Autocomplete remote machine names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| complete -o default -o nospace -W "$(/usr/bin/env ruby -ne 'puts $_.split(/[,\s]+/)[1..-1].reject{|host| host.match(/\*|\?/)} if $_.match(/^\s*Host\s+/);' < $HOME/.ssh/config)" scp sftp ssh | |
| _complete_hosts () { | |
| COMPREPLY=() | |
| cur="${COMP_WORDS[COMP_CWORD]}" | |
| host_list=`{ | |
| for c in /etc/ssh_config /etc/ssh/ssh_config ~/.ssh/config | |
| do [ -r $c ] && sed -n -e 's/^Host[[:space:]]//p' -e 's/^[[:space:]]*HostName[[:space:]]//p' $c | |
| done | |
| for k in /etc/ssh_known_hosts /etc/ssh/ssh_known_hosts ~/.ssh/known_hosts | |
| do [ -r $k ] && egrep -v '^[#\[]' $k|cut -f 1 -d ' '|sed -e 's/[,:].*//g' | |
| done | |
| sed -n -e 's/^[0-9][0-9\.]*//p' /etc/hosts; }|tr ' ' '\n'|grep -v '*'` | |
| COMPREPLY=( $(compgen -W "${host_list}" -- $cur)) | |
| return 0 | |
| } | |
| complete -F _complete_hosts host | |
| complete -F _complete_hosts ssh | |
| complete -F _complete_hosts scp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment