Repository navigation
Expand file tree
/
Copy pathbash-function-ssh
More file actions
116 lines (80 loc) · 2.56 KB
/
Copy pathbash-function-ssh
File metadata and controls
116 lines (80 loc) · 2.56 KB
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
#!/bin/bash
# loaded by "${HOME}"/.local/bin/bash-functions
# ssh ──────────────────────────────────────────────────────────────────────────
ssh () {
local host_destination
local argument_ssh
local argument_previous
local color_destination
local color_reset
local password_ssh
local status_ssh
local name_option
local file_identity
local host_key
# destination - first argument that is neither an option nor an option value
for argument_ssh in "${@}"
do
if [[ "${argument_ssh}" != -* ]] && [[ ! "${argument_previous}" =~ ^-[BbcDEeFIiJLlmOoPpQRSWw]$ ]]
then
host_destination="${argument_ssh}"
break
fi
argument_previous="${argument_ssh}"
done
# no destination - options only - plain ssh
if [ -z "${host_destination}" ]
then
command ssh "${@}"
return "${?}"
fi
# key host - an identity file ssh would offer exists - key login - no prompt
# session started after the loop - inside it stdin would be the ssh -G output
while read -r name_option file_identity
do
file_identity="${file_identity/#\~/${HOME}}"
if [ "${name_option}" = "identityfile" ] && [ -f "${file_identity}" ]
then
host_key="true"
break
fi
done < <(command ssh -G "${@}" 2> "${sink}")
if [ "${host_key}" = "true" ]
then
command ssh -X "${@}"
return "${?}"
fi
# password prompt colors: destination in yellow - then back to normal
color_destination="$(tput setaf 3)"
color_reset="$(tput sgr 0)"
echo -n -e "[ ${color_destination}${host_destination}${color_reset} ] >_ ? "
read -r -s password_ssh
echo
# empty password - plain ssh - key login and its own prompts
if [ -z "${password_ssh}" ]
then
command ssh -X "${@}"
return "${?}"
fi
# password passed through the environment - not shown in the process list
# new host keys accepted - changed host keys still refused
SSHPASS="${password_ssh}" \
sshpass -e \
ssh -X \
-o StrictHostKeyChecking=accept-new \
"${@}"
status_ssh="${?}"
unset password_ssh
# sshpass and ssh failures reported - other codes belong to the remote command
if [ "${status_ssh}" -eq 5 ]
then
frobulator.err "Login refused - incorrect password" "[ ${host_destination} ]"
elif [ "${status_ssh}" -eq 6 ]
then
frobulator.err "Host key unknown - not accepted" "[ ${host_destination} ]"
elif [ "${status_ssh}" -eq 255 ]
then
frobulator.err "Connection failed" "[ ${host_destination} ]"
fi
return "${status_ssh}"
}