aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/_ssh_config_hosts.bash
blob: 16265e8a83f2c62495cff7f6deae9126316e1f83 (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
25
26
27
28
29
30
31
# Complete ssh_config(5) hostnames
_ssh_config_hosts() {

    # Iterate through SSH client config paths
    local config
    for config in /etc/ssh/ssh_config.d/*.conf /etc/ssh/ssh_config \
            "$HOME"/.ssh/config.d/*.conf "$HOME"/.ssh/config ; do
        [[ -e $config ]] || continue

        # Read 'Host' options and their patterns from file
        local option value patterns pattern ci
        while read -r option value ; do
            [[ $option == Host ]] || continue
            read -a patterns -r \
                < <(printf '%s\n' "$value")

            # Check host value
            for pattern in "${patterns[@]}" ; do
                case $pattern in
                    # No empties
                    '') ;;
                    # No wildcards
                    *'*'*) ;;
                    # Found a match; print it
                    "$2"*) COMPREPLY[ci++]=$pattern ;;
                esac
            done

        done < "$config"
    done
}