summaryrefslogtreecommitdiff
path: root/contrib/ssh
blob: 85325aa2c2b86331d9f530a3599836422477ff93 (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- mode: shell-script; sh-basic-offset: 8; indent-tabs-mode: t -*-
# ex: ts=8 sw=8 noet filetype=sh
#
# ssh(1) completion
#
have ssh && {
_ssh()
{
	local cur prev
	local optconfigfile
	local -a config

	COMPREPLY=()
	cur=`_get_cword`
	prev=${COMP_WORDS[COMP_CWORD-1]}

	case "$prev" in
	-F)
		_filedir
		;;
	-*c)
	    COMPREPLY=( $( compgen -W 'blowfish 3des 3des-cbc blowfish-cbc \
			   arcfour cast128-cbc' -- $cur ) )
	    ;;
	-*i)
	    _filedir
	    ;;
	-*l)
	    COMPREPLY=( $( compgen -u -- $cur ) )
	    ;;
	*)
		# Search COMP_WORDS for '-F configfile' argument
		set -- "${COMP_WORDS[@]}"
		while [ $# -gt 0 ]; do
			if [ "${1:0:2}" = -F ]; then
				if [ ${#1} -gt 2 ]; then
					optconfigfile="$(dequote "$1")"
				else
					shift
					[ "$1" ] && optconfigfile="$(dequote "-F$1")"
				fi
				break
			fi
			shift
		done

	    _known_hosts -a "$optconfigfile"

	    [ $COMP_CWORD -eq 1 -o -n "$optconfigfile" ] || \
		COMPREPLY=( "${COMPREPLY[@]}" $( compgen -c -- $cur ) )
	esac

	return 0
}
shopt -u hostcomplete && complete -F _ssh ssh slogin sftp xhost autossh

# scp(1) completion
#
_scp()
{
	local cur userhost path
	local optconfigfile

	COMPREPLY=()
	cur=`_get_cword ":"`

	_expand || return 0

	if [[ "$cur" == *:* ]]; then
		local IFS=$'\t\n'
		# remove backslash escape from :
		cur=${cur/\\:/:}
		userhost=${cur%%?(\\):*}
		path=${cur#*:}
		# unescape spaces
		path=${path//\\\\\\\\ / }
		if [ -z "$path" ]; then
			# default to home dir of specified user on remote host
			path=$(ssh -o 'Batchmode yes' $userhost pwd 2>/dev/null)
		fi
		# escape spaces; remove executables, aliases, pipes and sockets;
		# add space at end of file names
		COMPREPLY=( $( ssh -o 'Batchmode yes' $userhost \
			       command ls -aF1d "$path*" 2>/dev/null | \
			       sed -e "s/[][(){}<>\",:;^&\!$=?\`|\\ ']/\\\\\\\\\\\\&/g" \
				   -e 's/[*@|=]$//g' -e 's/[^\/]$/& /g' ) )
		return 0
	fi

	# Search COMP_WORDS for '-F configfile' argument
	set -- "${COMP_WORDS[@]}"
	while [ $# -gt 0 ]; do
		if [ "${1:0:2}" = -F ]; then
			if [ ${#1} -gt 2 ]; then
				optconfigfile="$(dequote "$1")"
			else
				shift
				[ "$1" ] && optconfigfile="$(dequote "-F$1")"
			fi
			break
		fi
		shift
	done

	[[ "$cur" == */* ]] || _known_hosts -c -a "$optconfigfile"

	# This approach is used instead of _filedir to get a space appended
	# after local file/dir completions, and $nospace retained for others.
	local IFS=$'\t\n'
	COMPREPLY=( "${COMPREPLY[@]}" $( command ls -aF1d $cur* \
		    2>/dev/null | sed \
		    -e "s/[][(){}<>\",:;^&\!$=?\`|\\ ']/\\\\&/g" \
		    -e 's/[*@|=]$//g' -e 's/[^\/]$/& /g' ) )

	return 0
}
complete -F _scp $nospace scp

# ssh-copy-id(1) completion
#
_ssh_copy_id() {
	local cur prev

	COMPREPLY=()
	cur=`_get_cword`
	prev=${COMP_WORDS[COMP_CWORD-1]}

	case "$prev" in
	-*i)
	    _filedir
	    ;;
	*)
	    _known_hosts -a

	    [ $COMP_CWORD -eq 1 ] || \
		COMPREPLY=( "${COMPREPLY[@]}" $( compgen -- $cur ) )
	esac

	return 0
}
complete -F _ssh_copy_id $filenames ssh-copy-id
}