aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/_ssh_config_hosts.bash
blob: 3f937a2a634451f2cefd77b6f41884e6b41c24de (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 words from a subshell
    local ci comp
    while read -r comp ; do
        COMPREPLY[ci++]=$comp
    done < <(

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

            # Read 'Host' options and their first value from file
            while read -r option value _ ; do
                [[ $option == Host ]] || continue

                # Check host value
                case $value in
                    # No empties
                    ('') ;;
                    # No wildcards
                    (*'*'*) ;;
                    # Found a match; print it
                    ("$2"*) printf '%s\n' "$value" ;;
                esac

            done < "$config"
        done
    )
}