This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
ssh-diff
executable file
·269 lines (192 loc) · 7.42 KB
/
ssh-diff
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env bash
# +---------------------------------------------------------------------------------------------------+
# | Title : ssh-diff |
# | Description : Diff a file over SSH |
# | Author : Sven Wick <[email protected]> |
# | Contributors : Denis Meiswinkel |
# | URL : https://github.com/vaporup/ssh-tools |
# | Based On : https://gist.github.com/jhqv/dbd59f5838ae8c83f736bfe951bd80ff |
# | https://serverfault.com/questions/59140/how-do-diff-over-ssh#comment1027981_216496 |
# +---------------------------------------------------------------------------------------------------+
# shellcheck disable=SC2029
#
# Some colors for better output
#
RED='\033[0;91m'
BOLD='\033[1m'
RESET='\033[0m'
#
# Usage/Help message
#
function usage() {
cat << EOF
Usage: ${0##*/} [OPTIONS] FILE [user@]hostname[:FILE]
There is an extra roundtrip to the remote system
to check for the existence of the file to be diffed.
So if you are not using SSH Keys
you may get prompted twice for a password.
Use "CHECK_REMOTE_FILE_EXISTS=NO ${0##*/}" to disable that behavior
Diff Options:
All options your local diff command supports ( except '-r' ).
See 'man diff' and 'diff --help' for more information.
SSH Options:
-4 Use IPv4 only
-6 Use IPv6 only
-p port Port to connect to on the remote host.
This can be specified on a per-host basis in the configuration file.
Examples:
Default:
${0##*/} /etc/hosts 192.168.1.10
${0##*/} /etc/hosts [email protected]
${0##*/} /etc/hosts [email protected]:/etc/hosts.old
Side-by-Side:
${0##*/} -y /etc/hosts 192.168.1.10
${0##*/} -y /etc/hosts [email protected]
${0##*/} -y /etc/hosts [email protected]:/etc/hosts.old
Unified:
${0##*/} -u /etc/hosts 192.168.1.10
${0##*/} -u /etc/hosts [email protected]
${0##*/} -u /etc/hosts [email protected]:/etc/hosts.old
EOF
}
if [[ -z $1 || $1 == "--help" ]]; then
usage
exit 1
fi
if ! [[ $# -ge 2 ]]; then
echo -e "\n ${RED}Error: Not all filenames given${RESET}" >&2
usage
exit 1
fi
function supports_colordiff() {
type colordiff &> /dev/null
}
function show_header() {
echo ""
echo -e "Comparing ${BOLD}${remote_host}:${remote_file}${RESET} (<) with ${BOLD}${local_file}${RESET} (>)"
echo ""
}
function login_successful_and_remote_file_exists() {
# shellcheck disable=SC2154
if [[ ${CHECK_REMOTE_FILE_EXISTS} == "NO" ]]; then
return 0
fi
if [[ -z "${username}" ]]; then
ssh "${ssh_params[@]}" "${remote_host}" "test -e ${remote_file}" &> /dev/null
else
ssh "${ssh_params[@]}" "${username}@${remote_host}" "test -e ${remote_file}" &> /dev/null
fi
RETVAL=$?
[[ ${RETVAL} -eq 255 ]] && { echo -e "\n ${RED}Error: Could not connect to remote server${RESET}\n" >&2 ; exit "${RETVAL}"; }
[[ ${RETVAL} -ge 1 ]] && { echo -e "\n ${RED}Error: Remote file ${remote_file} not found${RESET}\n" >&2 ; exit "${RETVAL}"; }
[[ ${RETVAL} -eq 0 ]] && true
}
function diff_files() {
if [[ -z "${username}" ]]; then
if login_successful_and_remote_file_exists; then
show_header
if supports_colordiff; then
ssh "${ssh_params[@]}" "${remote_host}" "cat ${remote_file}" | diff "${diff_params[@]}" --label "${remote_host}:${remote_file}" - "${local_file}" | colordiff
else
# Pipe the output to cat after diffing
#
# test.sh fails when colordiff is not installed.
# Reason is that a normal diff returns with 1
# when files differ but colordiff changes the return code to 0.
ssh "${ssh_params[@]}" "${remote_host}" "cat ${remote_file}" | diff "${diff_params[@]}" --label "${remote_host}:${remote_file}" - "${local_file}" | cat
fi
fi
else
if login_successful_and_remote_file_exists; then
show_header
if supports_colordiff; then
ssh "${ssh_params[@]}" "${username}@${remote_host}" "cat ${remote_file}" | diff "${diff_params[@]}" --label "${remote_host}:${remote_file}" - "${local_file}" | colordiff
else
# Pipe the output to cat after diffing
#
# test.sh fails when colordiff is not installed.
# Reason is that a normal diff returns with 1
# when files differ but colordiff changes the return code to 0.
ssh "${ssh_params[@]}" "${username}@${remote_host}" "cat ${remote_file}" | diff "${diff_params[@]}" --label "${remote_host}:${remote_file}" - "${local_file}" | cat
fi
fi
fi
}
#
# MAIN
#
#
# Get all params from command line
#
params=( "$@" )
#
# Get last 2 params, store them away and remove them so only diff params are left
#
remote_params="${params[ ${#params[@]}-1 ]}" && unset 'params[ ${#params[@]}-1 ]'
local_filename="${params[ ${#params[@]}-1 ]}" && unset 'params[ ${#params[@]}-1 ]'
diff_params=( "${params[@]}" )
#
# Fish within diff params for ssh options and extract them
#
diff_params_index=0
ssh_params=()
ssh_params_indices=()
for param in "${diff_params[@]}"; do
next_diff_params_index=$(( diff_params_index +1 ))
#
# find -p <port>
#
if [[ ${param} == "-p" ]] && [[ -z "${diff_params[${next_diff_params_index}]//[0-9]}" ]]; then
ssh_params_indices+=( "${diff_params_index}" "${next_diff_params_index}" )
ssh_params+=( "${param}" "${diff_params[${next_diff_params_index}]}" )
fi
#
# find -4 or -6
#
if [[ ${param} == "-4" ]] || [[ ${param} == "-6" ]]; then
ssh_params_indices+=( "${diff_params_index}" )
ssh_params+=( "${param}" )
fi
diff_params_index=$(( diff_params_index +1 ))
done
# shellcheck disable=SC2034
for ssh_param in "${ssh_params_indices[@]}"; do
# shellcheck disable=SC2184
# shellcheck disable=SC2102
unset diff_params[$ssh_param]
done
#
# Getting username, hostname and filename from command line without using grep and awk
#
# user@host:filename -> user gets stored in $username
# -> host gets stored in $remote_host
# -> filename gets stored in $remote_file
#
if [[ ${remote_params} == *"@"* ]]; then
remote_part="${remote_params##*@}"
username="${remote_params%%@*}"
else
remote_part=${remote_params}
fi
if [[ ${remote_part} == *":"* ]]; then
remote_file="${remote_part##*:}"
remote_host="${remote_part%%:*}"
else
remote_host=${remote_part}
fi
local_file=$(readlink -f "${local_filename}") # get absolute path to file in case it was relative
if [[ -z "${local_file}" ]]; then
echo -e "\n ${RED}Error: Given file not found${RESET}\n" >&2
exit 1
fi
if [[ ! -f ${local_file} ]]; then
echo -e "\n ${RED}Error: Local file ${local_file} not found${RESET}\n" >&2
exit 1
fi
if [[ -z "${remote_file}" ]]; then
remote_file=${local_file}
fi
#
# Finally diff them!
#
diff_files