forked from aaronhurt/zfs-replicate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncgate.sh
executable file
·89 lines (76 loc) · 1.96 KB
/
ncgate.sh
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
#!/bin/bash
#
# Send stdin via netcat to some target
# This is an UNENCRYPTED stream, use with caution!
#
# This script will ssh to the target to start a netcat
# server to than send the data.
#
COMMAND=""
TARGET=""
PORT=2222
usage() {
printf "usage: $0 -t [user@target] -c [command] -p [port [2222]]\n"
printf " -t target ip/host\n"
printf " -c command used to handle receiving data (data will be piped into it)\n"
printf " e.g. \"> /tmp/file\" would create a file containing the sent content.\n"
printf " -p port used to start the remote server\n"
printf "\n\n"
printf "Examples\n"
printf "========\n"
printf "Sending a file:\n"
printf " $0 -t <target> -c \"> /tmp/test.file\" < /tmp/test.file\n"
printf "\n"
printf "Sending/Receiving a zfs snapshot:\n"
printf " zfs send tank/dana@snap1 | $0 -t <target> -c \"| zfs recv newtank/dana\"\n"
}
while [ $# -gt 0 ]; do
case $1 in
"-t")
TARGET="$2"
;;
"-p")
PORT="$2"
;;
"-c")
COMMAND="$2"
;;
**)
usage
exit 1
;;
esac
shift;shift
done
if [ -z "${COMMAND}" ] || [ -z "${TARGET}" ] || [ -z "${PORT}" ]; then
usage
exit 1
fi
tmp_name="$(date +%s)"
REMOTE_LOG="/tmp/ncgate-${tmp_name}-server.log"
REMOTE_CODE="/tmp/ncgate-${tmp_name}-server.code"
printf "Spawning remote server...\n"
(ssh ${TARGET} "nc -l ${PORT} ${COMMAND} 2>&1" > "${REMOTE_LOG}"; echo $? > "${REMOTE_CODE}") &
pid=$!
printf "Server running with pid: ${pid}\n"
sleep 1.5
printf "Sending data...\n"
cat - | nc -N ${TARGET} ${PORT}
sleep 1.5
# remote output
if [ -f "${REMOTE_LOG}" ]; then
log=$(cat "${REMOTE_LOG}")
rm "${REMOTE_LOG}"
fi
# remote exit code
code=-1
if [ -f "${REMOTE_CODE}" ]; then
code=$(cat "${REMOTE_CODE}")
rm "${REMOTE_CODE}"
fi
if [ $code -eq 0 ]; then
printf "$log\n" 1>&2
else
printf "$log\n"
fi
exit $code