-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtscp
executable file
·157 lines (137 loc) · 4.13 KB
/
tscp
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
#!/bin/bash
#
# Secure copy files using 'scp' through 'ssh' tunnel.
# The tscp utility exits 0 on success, and >0 if an error occurs.
#
# Source code: https://github.com/Datamart/tscp
# Style guide: https://google.github.io/styleguide/shell.xml
#
# Copyright 2015 Datamart Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
readonly SELF_NAME=$(basename $0)
readonly MIN_BIND_PORT=1024
readonly MAX_BIND_PORT=32767
readonly BIND_PORT=$(($RANDOM % ($MAX_BIND_PORT - $MIN_BIND_PORT + 1) + $MIN_BIND_PORT))
readonly SOCKET_PATH="${TMPDIR}${SELF_NAME}-session-${BIND_PORT}"
readonly CMD_ARGS=("$@")
readonly LEN_ARGS="${#CMD_ARGS[@]}"
#
# Prints usage message and exits.
# Globals:
# SELF_NAME
#
function usage() {
echo "usage: ${SELF_NAME} -g [user@]gateway.host" >&2
echo " [user@]remote.host:source [[user@]target.host:]target" >&2
exit 1
}
#
# Creates ssh tunnel, copies files and closes ssh tunnel.
# Globals:
# BIND_PORT
# SOCKET_PATH
#
function run() {
local GATEWAY=$1
local REMOTE_USER=$2
local REMOTE_HOST=$3
local SOURCE=$4
local TARGET=$5
# -f Requests ssh to go to background just before command execution.
# -M Places the ssh client into 'master' mode for connection sharing.
# -N Do not execute a remote command.
# -L [bind_address:]port:host:hostport Specifies that the given port on the
# local (client) host is to be forwarded to the given host and port on the
# remote side.
# -S ctl_path Specifies the location of a control socket for connection sharing.
# -O ctl_cmd Control an active connection multiplexing master process
# echo "Creating SSH tunnel."
ssh -f -N -M -S "${SOCKET_PATH}" -L "${BIND_PORT}:${REMOTE_HOST}:22" "${GATEWAY}"
if [[ "$?" -ne 0 ]]; then
echo "Unable to create SSH tunnel." >&2
exit 1
fi
# echo "Coping file."
scp -P "${BIND_PORT}" "${REMOTE_USER}@127.0.0.1:${SOURCE}" "${TARGET}"
if [[ "$?" -ne 0 ]]; then
echo "Unable to copy file." >&2
exit 1
fi
# echo "Closing SSH tunnel."
ssh -S "${SOCKET_PATH}" -O exit "${GATEWAY}"
if [[ "$?" -ne 0 ]]; then
echo "Unable to close SSH tunnel." >&2
exit 1
fi
}
#
# Gets gateway URI as 'username@gateway.host' format.
# If username is omitted, current logged-in user will be used.
#
function get_gateway() {
local GATEWAY=
while [[ $# > 1 ]]; do
key="$1"
case $key in
-g|--gateway)
GATEWAY="$2"
shift
;;
esac
shift
done
if [[ ! -z "$GATEWAY" ]]; then
local GATEWAY_LIST=(${GATEWAY//@/ })
local GATEWAY_USER="${GATEWAY_LIST[0]}"
local GATEWAY_HOST="${GATEWAY_LIST[1]}"
# Covers gateway uri without username.
if [[ -z $GATEWAY_HOST ]]; then
GATEWAY_HOST="${GATEWAY_USER}"
GATEWAY_USER="${USER}"
fi
GATEWAY="${GATEWAY_USER}@${GATEWAY_HOST}"
fi
echo "${GATEWAY}"
}
#
# The main function.
# Globals:
# USER
# CMD_ARGS
# LEN_ARGS
#
function main() {
if [[ ${LEN_ARGS} -ge 4 ]]; then
local TARGET=${CMD_ARGS[${LEN_ARGS} - 1]}
local SOURCE=${CMD_ARGS[${LEN_ARGS} - 2]}
local GATEWAY=$(get_gateway "$@")
readonly REMOTE_LIST=(${SOURCE//@/ })
local REMOTE_USER="${REMOTE_LIST[0]}"
local REMOTE_PATH="${REMOTE_LIST[1]}"
# Covers remote uri without username.
if [[ -z $REMOTE_PATH ]]; then
REMOTE_PATH="${REMOTE_USER}"
REMOTE_USER="${USER}"
fi
readonly SOURCE_LIST=(${REMOTE_PATH//:/ })
local REMOTE_HOST="${SOURCE_LIST[0]}"
local SOURCE_PATH="${SOURCE_LIST[1]}"
if [[ -n $GATEWAY ]] && [[ -n $SOURCE_PATH ]] && [[ -n $TARGET ]]; then
run ${GATEWAY} ${REMOTE_USER} ${REMOTE_HOST} ${SOURCE_PATH} ${TARGET}
exit 0
fi
fi
usage
}
main "$@"