2022-01-15 10:00:35 +00:00
|
|
|
#! bash oh-my-bash.module
|
2017-10-10 11:07:01 +00:00
|
|
|
# Bash completion support for ssh.
|
|
|
|
|
2023-09-30 19:44:05 +00:00
|
|
|
_omb_module_require lib:omb-completion
|
2017-10-10 11:07:01 +00:00
|
|
|
|
2023-09-30 18:19:46 +00:00
|
|
|
function _omb_completion_ssh {
|
2023-09-30 19:44:05 +00:00
|
|
|
local cur
|
|
|
|
_omb_completion_reassemble_breaks :
|
|
|
|
|
2024-05-02 00:26:02 +00:00
|
|
|
local -a options
|
2023-09-30 18:40:52 +00:00
|
|
|
if [[ $cur == *@* ]] ; then
|
2024-05-02 00:26:02 +00:00
|
|
|
options=(-P "${cur%%@*}@" -- "${cur#*@}")
|
2023-09-30 18:32:03 +00:00
|
|
|
else
|
2024-05-02 00:26:02 +00:00
|
|
|
options=(-- "$cur")
|
2023-09-30 18:32:03 +00:00
|
|
|
fi
|
|
|
|
|
2024-04-28 04:21:12 +00:00
|
|
|
local IFS=$'\n'
|
|
|
|
|
2024-05-16 09:57:23 +00:00
|
|
|
local base_config_file
|
2024-05-08 18:31:51 +00:00
|
|
|
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
|
2024-05-16 09:57:23 +00:00
|
|
|
basedir=${base_config_file%/*}
|
2024-05-08 18:31:51 +00:00
|
|
|
local -a config_files=("$base_config_file")
|
|
|
|
|
|
|
|
# check if config file contains Include options
|
|
|
|
local -a include_patterns
|
2024-05-16 09:57:23 +00:00
|
|
|
_omb_util_split include_patterns "$(awk -F' ' '/^Include/{print $2}' "$base_config_file" 2>/dev/null)" $'\n'
|
2024-05-08 18:31:51 +00:00
|
|
|
local i
|
|
|
|
for i in "${!include_patterns[@]}"; do
|
|
|
|
# relative or absolute path, if relative transforms to absolute
|
2024-05-16 10:00:01 +00:00
|
|
|
[[ ${include_patterns[i]} == /* ]] || include_patterns[i]=$basedir/${include_patterns[i]}
|
2024-05-08 18:31:51 +00:00
|
|
|
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
|
2024-02-20 13:33:46 +00:00
|
|
|
|
2024-05-08 18:31:51 +00:00
|
|
|
COMPREPLY+=($(compgen -W "$(awk '/^Host/ {for (i=2; i<=NF; i++) print $i}' "${config_files[@]}")" "${options[@]}"))
|
2017-10-10 11:07:01 +00:00
|
|
|
fi
|
2024-05-08 18:31:51 +00:00
|
|
|
done
|
|
|
|
|
2024-05-16 09:57:23 +00:00
|
|
|
local known_hosts_file
|
2024-05-08 18:31:51 +00:00
|
|
|
for known_hosts_file in ~/.ssh/known_hosts /etc/ssh/ssh_known_hosts; do
|
|
|
|
if [[ -r $known_hosts_file ]]; then
|
2024-05-16 10:00:01 +00:00
|
|
|
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[@]}"))
|
2024-05-08 18:31:51 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2017-10-10 11:07:01 +00:00
|
|
|
|
2023-09-30 18:32:03 +00:00
|
|
|
# parse hosts defined in /etc/hosts
|
2023-09-30 18:40:52 +00:00
|
|
|
if [[ -r /etc/hosts ]]; then
|
2024-05-16 10:00:01 +00:00
|
|
|
COMPREPLY+=($(compgen -W "$(grep -v '^[[:space:]]*$' /etc/hosts | grep -v '^#' | awk '{for (i=2; i<=NF; i++) print $i}' )" "${options[@]}"))
|
2023-09-30 18:32:03 +00:00
|
|
|
fi
|
2017-10-10 11:07:01 +00:00
|
|
|
|
2023-09-30 19:44:05 +00:00
|
|
|
_omb_completion_resolve_breaks
|
2017-10-10 11:07:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-30 18:19:46 +00:00
|
|
|
complete -o default -o nospace -F _omb_completion_ssh ssh scp
|