aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/ssh.bash
blob: 91d83e359360231fd116ce03f33d3b820a483c17 (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
# Wrap scp to check for missing colons
scp() {
    if (($# >= 2)) && [[ $* != *:* ]] ; then
        printf 'scp: Missing colon, probably an error\n' >&2
        return 1
    fi
    command scp "$@"
}

# Completion for ssh/sftp/ssh-copy-id with config hostnames
_ssh() {
    local word=${COMP_WORDS[COMP_CWORD]}

    # Read hostnames from existent config files, no asterisks
    local -a hosts
    local config option value
    for config in "$HOME"/.ssh/config /etc/ssh/ssh_config ; do
        if [[ -e $config ]] ; then
            while read -r option value _ ; do
                if [[ $option == Host && $value != *'*'* ]] ; then
                    hosts=("${hosts[@]}" "$value")
                fi
            done < "$config"
        fi
    done

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