74 lines
1.7 KiB
Bash
Executable File
74 lines
1.7 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
|
|
# (1) Init.
|
|
#--------------------------------------------------------#
|
|
# (1) Get current absolute dir
|
|
ROOT=$(dirname `realpath $0`);
|
|
|
|
# (2) Check argc
|
|
test $# -lt 2 && echo -e "error: too fewarguments\n\n\e[1mUSAGE\e[0m\n\tbind-output <id> <ip> <port>\n\n\e[1mARGUMENTS\e[0m\n\t<id>\tThe identifier of the output (buffer name)\n\t<ip>\tThe ip or host of the target\n\t<port>\tThe port to connect to\n" && exit 1;
|
|
|
|
# (3) Check @PORT range #
|
|
MIN_PORT=1024;
|
|
MAX_PORT=49151;
|
|
test "$3" -gt "$MAX_PORT" -o "$3" -lt "$MIN_PORT" && echo "error: <port> must be between $MIN_PORT and $MAX_PORT" && exit 1;
|
|
|
|
# (4) Set argument explicit names #
|
|
OUT_ID="$1";
|
|
OUT_IP="$2";
|
|
OUT_PT="$3";
|
|
|
|
|
|
|
|
# (2) Prepare buffer
|
|
#--------------------------------------------------------#
|
|
# (1) Create buffer path
|
|
BUFFER="/tmp/outbuf_$OUT_ID";
|
|
|
|
# (2) Create/Flush buffer
|
|
test -f $BUFFER && rm $BUFFER;
|
|
touch $BUFFER;
|
|
|
|
|
|
|
|
|
|
# (3) Launch INPUT server
|
|
#--------------------------------------------------------#
|
|
(
|
|
|
|
trap "kill -9 %1 2>/dev/null" INT KILL;
|
|
outfail=0;
|
|
|
|
# (1) kill script after 10 failures
|
|
while [ $outfail -lt 10 ]; do
|
|
|
|
# (2) bind socket to buffer
|
|
echo "(.) binding $BUFFER to $OUT_IP:$OUT_PT";
|
|
|
|
# (3) Check if port open
|
|
nc $OUT_IP $OUT_PT < /dev/null;
|
|
PORT_CLOSED=$?;
|
|
|
|
# (4) Manage taken port #
|
|
if [ "$PORT_CLOSED" -eq "1" ]; then
|
|
outfail="`expr $outfail + 1`";
|
|
sleep .5;
|
|
continue;
|
|
fi;
|
|
|
|
# (5) Bind output
|
|
outfail="0";
|
|
( tail -f $BUFFER | nc $OUT_IP $OUT_PT 2> /dev/null || echo " * cannot access $OUT_IP:$OUT_PT" )&
|
|
wait $!;
|
|
|
|
done;
|
|
|
|
)&
|
|
LOOP_PID=$!;
|
|
|
|
|
|
# (2) Remove buffer + kill loop #
|
|
trap "rm $BUFFER 2>/dev/null; kill -9 $LOOP_PID 2>/dev/null" INT KILL;
|
|
|
|
wait $LOOP_PID; |