-
Notifications
You must be signed in to change notification settings - Fork 2
/
recv.sh
executable file
·204 lines (168 loc) · 2.99 KB
/
recv.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
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
#!/bin/bash
SELF="$0"
VERSION="0.0.1"
NULL=/dev/null
STDIN=0
STDOUT=1
STDERR=2
if [ -z "$TMPDIR" ]; then
TMPDIR="/tmp"
fi
if [ -t 0 ]; then
ISATTY=1
else
ISATTY=0
fi
throw () {
{
printf "recv: error: "
echo "$@"
} >&$STDERR
exit 1
}
verbose () {
if [ "1" = "$VERBOSE" ]; then
printf "recv: verbose: "
printf "$@"
printf "\n"
fi
}
version () {
echo $VERSION
}
usage () {
echo "usage: umq recv <host> <port> [-hsvV] [-f <file>]"
if [ "$1" = "1" ]; then
echo
echo "examples:"
echo "$ umq recv localhost 3000 | { \\
while read -r ch; do echo \"\$ch\"; done"
echo
echo "options:"
echo " -f, --file <file> file to execute on each chunk received"
echo " -s, --single close after first connection"
echo " -v, --verbose show verbose output"
echo " -h, --help display this message"
echo " -V, --version output version"
fi
}
if [ "${1:0:1}" != "-" ]; then
host="$1"
shift
fi
if [ "${1:0:1}" != "-" ]; then
port="$1"
shift
else
port="$host"
fi
if [ -z "$port" ]; then
port="$host"
fi
if [ -z "$host" ]; then
host=""
fi
while true; do
arg="$1"
if [ "" = "$1" ]; then
break;
fi
if [ "${arg:0:1}" != "-" ]; then
shift
continue
fi
case $arg in
-f|--file)
FILE="$2"
shift 2
;;
-s|--single)
SINGLE=1
shift
;;
-v|--verbose)
VERBOSE=1
shift
;;
-h|--help)
usage 1
exit 1
;;
-V|--version)
version
exit 0
;;
*)
{
echo "unknown option \`$arg'"
} >&$STDERR
usage
exit 1
;;
esac
done
opts=""
if [ "$host" != "$port" ]; then
args="$opts"
cmd="nc $host $port $args"
else
if [ "1" != "$SINGLE" ]; then
opts="$opts -k "
fi
args="$opts-l $port"
cmd="nc $args"
fi
verbose "args: $args"
if [ ! -z "$FILE" ]; then
if ! test -f "$FILE"; then
throw "'$FILE' doesn't exist"
fi
fi
if [ "$host" = "$port" ]; then
MAKEPIPE=1
pipe="$TMPDIR/_nc_fifo.$host.$port"
rm -f "$pipe"
mkfifo "$pipe"
verbose "pipe: $pipe"
else
MAKEPIPE=0
fi
verbose "port: $port"
verbose "cmd: $cmd"
{
echo
if [ "1" = "$MAKEPIPE" ]; then
while test -p "$pipe"; do
cat $pipe;
done | $cmd | {
trap "echo exit; rm -f $pipe; exit" SIGINT SIGTERM
while read -r line; do
if [ ! -z "$FILE" ]; then
chunk=`echo "$line" | CHUNK="$line" "$FILE"`
else
chunk="$line"
fi
if [ "" != "$chunk" ]; then
echo "$chunk"
if [ "1" = "$MAKEPIPE" ]; then
printf "%s\n" "$chunk" >$pipe
fi
fi
done
}
else
while true; do
while read -r chunk; do
if [ ! -z "$FILE" ]; then
chunk=`echo "$chunk" | CHUNK="$chunk" "$FILE"`
fi
if [ "" != "$chunk" ]; then
echo "$chunk"
fi
$cmd
done < <(echo | $cmd)
done
fi
}
rm -f "$pipe"
exit $?