Skip to content

Commit

Permalink
Rebase and incorporate Includes, iterate over possible files
Browse files Browse the repository at this point in the history
  • Loading branch information
plasmastorm committed May 10, 2024
1 parent 4d256f9 commit 3520883
Showing 1 changed file with 30 additions and 37 deletions.
67 changes: 30 additions & 37 deletions completions/ssh.completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,42 @@ function _omb_completion_ssh {

local IFS=$'\n'

# parse all defined hosts from .ssh/config
if [[ -r ~/.ssh/config ]]; then
local -a config_files=(~/.ssh/config)
for base_config_file in ~/.ssh/config /etc/ssh/ssh_config; do
# parse all defined hosts from config file
if [[ -r $base_config_file ]]; then
local basedir
basedir=$(dirname $base_config_file)
local -a config_files=("$base_config_file")

# check if .ssh/config contains Include options
local -a include_patterns
_omb_util_split include_patterns "$(awk -F' ' '/^Include/{print $2}' ~/.ssh/config 2>/dev/null)" $'\n'
local i
for i in "${!include_patterns[@]}"; do
# relative or absolute path, if relative transforms to absolute
[[ ${include_patterns[i]} == /* ]] || include_patterns[i]=~/.ssh/${include_patterns[i]}
done
# check if config file contains Include options
local -a include_patterns
_omb_util_split include_patterns "$(awk -F' ' '/^Include/{print $2}' ${base_config_file} 2>/dev/null)" $'\n'
local i
for i in "${!include_patterns[@]}"; do
# relative or absolute path, if relative transforms to absolute
[[ ${include_patterns[i]} == /* ]] || include_patterns[i]=${basedir}/${include_patterns[i]}
done

# interpret possible globbing
local -a include_files
_omb_util_glob_expand include_files '${include_patterns[*]}'
local include_file
for include_file in "${include_files[@]}";do
# parse all defined hosts from that file
[[ -s $include_file ]] && config_files+=("$include_file")
done
# interpret possible globbing
local -a include_files
_omb_util_glob_expand include_files '${include_patterns[*]}'
local include_file
for include_file in "${include_files[@]}";do
# parse all defined hosts from that file
[[ -s $include_file ]] && config_files+=("$include_file")
done

COMPREPLY+=($(compgen -W "$(awk '/^Host/ {for (i=2; i<=NF; i++) print $i}' "${config_files[@]}")" "${options[@]}"))
fi

# parse all hosts found in .ssh/known_hosts
if [[ -r ~/.ssh/known_hosts ]]; then
if grep -v -q -e '^ ssh-rsa' ~/.ssh/known_hosts; then
COMPREPLY+=($(compgen -W "$( awk '{print $1}' ~/.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[@]}"))
COMPREPLY+=($(compgen -W "$(awk '/^Host/ {for (i=2; i<=NF; i++) print $i}' "${config_files[@]}")" "${options[@]}"))
fi
fi
done

# parse all defined hosts from /etc/ssh/ssh_config
if [[ -r /etc/ssh/ssh_config ]]; then
COMPREPLY+=($(compgen -W "$(grep ^Host "/etc/ssh/ssh_config" | awk '{for (i=2; i<=NF; i++) print $i}' )" "${options[@]}"))
fi

# parse all hosts found in /etc/ssh/ssh_known_hosts
if [[ -r /etc/ssh/ssh_known_hosts ]]; then
if grep -v -q -e '^ ssh-rsa' "/etc/ssh/ssh_known_hosts" ; then
COMPREPLY+=($(compgen -W "$( awk '{print $1}' "/etc/ssh/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[@]}"))
for known_hosts_file in ~/.ssh/known_hosts /etc/ssh/ssh_known_hosts; do
if [[ -r $known_hosts_file ]]; then
if grep -v -q -e '^ ssh-rsa' "${known_hosts_file}"; then
COMPREPLY+=($(compgen -W "$( awk '{print $1}' "${known_hosts_file}" | grep -v ^\| | cut -d, -f 1 | sed -e 's/\[//g' | sed -e 's/\]//g' | cut -d: -f1 | grep -v ssh-rsa)" "${options[@]}"))
fi
fi
fi
done

# parse hosts defined in /etc/hosts
if [[ -r /etc/hosts ]]; then
Expand Down

0 comments on commit 3520883

Please sign in to comment.