Better SSH completion.
[profile.git] / .profile.d / completion.bashrc
index 775c208..782cb69 100644 (file)
@@ -1,8 +1,11 @@
+# Old bash doesn't have completion support.
+if ! builtin complete 2>&1 | grep 'not a shell builtin' >/dev/null; then
+
 # Helper!
-export COMPLETION_DIR_SSH=~/.ssh/hosts
-export COMPLETION_DIR_TELNET=~/.telnet/hosts
-export COMPLETION_DIR_RDP=~/.rdp/hosts
-export COMPLETION_DIR_PING="$COMPLETION_DIR_SSH $COMPLETION_DIR_TELNET $COMPLETION_DIR_RDP"
+COMPLETION_DIR_SSH=${PROFILE_HOME:-~}/.ssh/hosts
+COMPLETION_DIR_TELNET=${PROFILE_HOME:-~}/.telnet/hosts
+COMPLETION_DIR_RDP=${PROFILE_HOME:-~}/.rdp/hosts
+COMPLETION_DIR_PING="$COMPLETION_DIR_SSH $COMPLETION_DIR_TELNET $COMPLETION_DIR_RDP"
 
 function _generic_completion() {
   COMPLETION_DIR=${1+"$@"}
@@ -35,6 +38,27 @@ function _known_hosts() {
   return 0
 }
 
+# Host completion from dsh groups.
+function _dshgroup() {
+  cur=${COMP_WORDS[COMP_CWORD]}
+
+  HOSTS=$(grep -rh '^[A-Za-z0-9]' ~/.dsh/group)
+
+  COMPREPLY=($(compgen -W "$HOSTS" -- "$cur"))
+  return 0
+}
+
+function _omnissh() {
+  cur=${COMP_WORDS[COMP_CWORD]}
+
+  HOSTS="$(grep -rh '^[A-Za-z0-9]' ~/.dsh/group)
+$(sed 's/[      ].*//;s/,/\n/' ~/.ssh/known_hosts)
+$(find $COMPLETION_DIR_SSH -type f -perm -100 2>/dev/null | sed 's@.*/@@')"
+
+  COMPREPLY=($(compgen -W "$HOSTS" -- "$cur"))
+  return 0
+}
+
 # Process completion for kill, strace etc.
 function _pid() {
   cur=${COMP_WORDS[COMP_CWORD]}
@@ -111,9 +135,9 @@ function _global() {
   return 0
 }
 
-complete -F _ssh rollout
-complete -F _ssh ssh
-complete -F _ssh sshterm
+complete -F _omnissh rollout
+complete -F _omnissh ssh
+complete -F _omnissh sshterm
 complete -F _ping telnet
 complete -F _telnet telnetterm
 complete -F _rdp rdesktop
@@ -126,6 +150,9 @@ complete -F _pid kill
 complete -F _pid strace
 complete -F _known_hosts kill_known_host
 complete -F _known_hosts knh
+complete -F _omnissh knh
 complete -F _modprobe modprobe
 complete -F _rmmod rmmod
 complete -F _global global
+
+fi