aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/ssh.bash
blob: c382bb17a2bf08ef76cd3959ccd98fb3ebe04b8c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Completion for ssh/sftp/ssh-copy-id with config hostnames
_ssh() {
    local config=$HOME/.ssh/config
    local word=${COMP_WORDS[COMP_CWORD]}
    local -a hosts

    # Bail if the configuration file is illegible
    if [[ ! -r $config ]]; then
        return 1
    fi

    # Read hostnames from the file, no asterisks
    local option value
    while read -r option value _; do
        if [[ $option == Host && $value != *'*'* ]]; then
            hosts=("${hosts[@]}" "$value")
        fi
    done < "$config"

    # Generate completion reply
    COMPREPLY=( $(compgen -W "${hosts[*]}" -- "$word") )
}
complete -F _ssh ssh sftp ssh-copy-id