Skip to content

Commit

Permalink
bring to compliance: ssh.completion
Browse files Browse the repository at this point in the history
  • Loading branch information
seefood committed Feb 1, 2025
1 parent e2a0593 commit 1010f41
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions completion/available/ssh.completion.bash
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env bash
# shellcheck shell=bash
# Bash completion support for ssh.

export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/}

_sshcomplete() {
local CURRENT_PROMPT="${COMP_WORDS[COMP_CWORD]}"
local line CURRENT_PROMPT="${COMP_WORDS[COMP_CWORD]}"
if [[ ${CURRENT_PROMPT} == *@* ]]; then
local OPTIONS="-P ${CURRENT_PROMPT/@*/}@ -- ${CURRENT_PROMPT/*@/}"
else
Expand All @@ -17,20 +17,21 @@ _sshcomplete() {
| awk '{for (i=2; i<=NF; i++) print $i}' \
| sed -Ee "s|^([^/~])|$HOME/.ssh/\1|" -e "s|^~/|$HOME/|"); do
if [ -r "$fl" ]; then
COMPREPLY=(${COMPREPLY[@]} $(compgen -W "$(grep -i ^Host "$fl" | grep -v '[*!]' | awk '{for (i=2; i<=NF; i++) print $i}')" ${OPTIONS}))
#shellcheck disable=SC2086
while IFS='' read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$(grep -i ^Host "$fl" | grep -v '[*!]' | awk '{for (i=2; i<=NF; i++) print $i}')" ${OPTIONS})
fi
done

# parse all hosts found in .ssh/known_hosts
if [ -r "$HOME/.ssh/known_hosts" ]; then
if grep -v -q -e '^ ssh-rsa' "$HOME/.ssh/known_hosts"; then
COMPREPLY=(${COMPREPLY[@]} $(compgen -W "$(awk '{print $1}' "$HOME/.ssh/known_hosts" | grep -v ^\| | cut -d, -f 1 | sed -e 's/\[//g' | sed -e 's/\]//g' | cut -d: -f1 | grep -v ssh-rsa)" ${OPTIONS}))
fi
if [ -r "$HOME/.ssh/known_hosts" ] && grep -v -q -e '^ ssh-rsa' "$HOME/.ssh/known_hosts"; then
#shellcheck disable=SC2086
while IFS='' read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$(awk '{print $1}' "$HOME/.ssh/known_hosts" | grep -v ^\| | cut -d, -f 1 | sed -e 's/\[//g' | sed -e 's/\]//g' | cut -d: -f1 | grep -v ssh-rsa)" ${OPTIONS})
fi

# parse hosts defined in /etc/hosts
if [ -r /etc/hosts ]; then
COMPREPLY=(${COMPREPLY[@]} $(compgen -W "$(grep -v '^[[:space:]]*$' /etc/hosts | grep -v '^#' | awk '{for (i=2; i<=NF; i++) print $i}')" ${OPTIONS}))
#shellcheck disable=SC2086
while IFS='' read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$(grep -v '^[[:space:]]*$' /etc/hosts | grep -v '^#' | awk '{for (i=2; i<=NF; i++) print $i}')" ${OPTIONS})
fi

return 0
Expand Down

0 comments on commit 1010f41

Please sign in to comment.