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
|
2024-02-22 09:27:44 +00:00
|
|
|
local additional_include_option
|
2024-02-22 08:37:02 +00:00
|
|
|
local additional_include_defined_file
|
2023-09-30 19:44:05 +00:00
|
|
|
_omb_completion_reassemble_breaks :
|
|
|
|
|
2023-09-30 18:40:52 +00:00
|
|
|
if [[ $cur == *@* ]] ; then
|
|
|
|
local -a options=(-P "${cur%%@*}@" -- "${cur#*@}")
|
2023-09-30 18:32:03 +00:00
|
|
|
else
|
2023-09-30 18:40:52 +00:00
|
|
|
local -a options=(-- "$cur")
|
2023-09-30 18:32:03 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# parse all defined hosts from .ssh/config
|
2023-09-30 18:40:52 +00:00
|
|
|
if [[ -r $HOME/.ssh/config ]]; then
|
|
|
|
COMPREPLY=($(compgen -W "$(grep ^Host "$HOME/.ssh/config" | awk '{for (i=2; i<=NF; i++) print $i}' )" "${options[@]}"))
|
2023-09-30 18:32:03 +00:00
|
|
|
fi
|
|
|
|
|
2024-02-22 08:37:02 +00:00
|
|
|
# check if .ssh/config contains Include options
|
2024-02-22 09:27:44 +00:00
|
|
|
for additional_include_option in $(awk -F' ' '/^Include/{print $2}' "$HOME/.ssh/config" 2>/dev/null) ;do
|
|
|
|
# relative or absolute path, if relative transforms to absolute
|
|
|
|
[[ "${additional_include_option:0:1}" == "/" ]] ||additional_include_option="$HOME/.ssh/$additional_include_option"
|
|
|
|
# for loop to interpret possible globbing
|
|
|
|
for additional_include_defined_file in $additional_include_option ;do
|
|
|
|
# parse all defined hosts from that file
|
|
|
|
[[ -s "$additional_include_defined_file" ]] &&COMPREPLY+=($(compgen -W "$(grep ^Host "$additional_include_defined_file" | awk '{for (i=2; i<=NF; i++) print $i}' )" "${options[@]}"))
|
|
|
|
done
|
2024-02-22 08:37:02 +00:00
|
|
|
done
|
2024-02-20 13:33:46 +00:00
|
|
|
|
2023-09-30 18:32:03 +00:00
|
|
|
# parse all hosts found in .ssh/known_hosts
|
2023-09-30 18:40:52 +00:00
|
|
|
if [[ -r $HOME/.ssh/known_hosts ]]; then
|
2023-09-30 18:32:03 +00:00
|
|
|
if grep -v -q -e '^ ssh-rsa' "$HOME/.ssh/known_hosts" ; then
|
2023-09-30 18:40:52 +00:00
|
|
|
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[@]}"))
|
2017-10-10 11:07:01 +00:00
|
|
|
fi
|
2023-09-30 18:32:03 +00:00
|
|
|
fi
|
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
|
|
|
|
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
|