updated bind-input/output to be silent by default (can be activated with optional last param: 'debug')

This commit is contained in:
xdrm-brackets 2018-02-01 00:21:34 +01:00
parent 650dc76324
commit 534b5a97ac
2 changed files with 20 additions and 17 deletions

View File

@ -18,6 +18,11 @@ test "$2" -gt "$MAX_PORT" -o "$2" -lt "$MIN_PORT" && echo "error: <port> must be
IN_ID="$1";
IN_PT="$2";
# (5) Manage optional 'silent' argument #
DEBUG="";
test $# -ge 3 -a "$3" == "debug" && DEBUG="xx";
# (2) Prepare buffer
@ -26,8 +31,8 @@ IN_PT="$2";
BUFFER="/tmp/inbuf_$IN_ID";
# (2) Create/Flush buffer
test -f $BUFFER && rm $BUFFER;
touch $BUFFER;
# test -f $BUFFER && rm $BUFFER;
echo -n "" > $BUFFER;
@ -36,7 +41,7 @@ touch $BUFFER;
#--------------------------------------------------------#
(
echo " > binding port $IN_PT to $BUFFER";
[[ $DEBUG ]] && echo " > binding port $IN_PT to $BUFFER";
trap "kill -9 $NC_PID 2>/dev/null; exit 1;" HUP INT KILL TERM;
infail=0;
@ -44,9 +49,6 @@ touch $BUFFER;
# (1) kill script after 10 failures
while [ $infail -lt 10 ]; do
# (2) bind socket to buffer
# echo " + $IN_PT -> $BUFFER";
# (3) Check if port already in use
ss -tl4 "( sport = :$IN_PT )" | grep "$IN_PT" >/dev/null;
PORT_TAKEN=$?;
@ -73,8 +75,8 @@ LOOP_PID=$!;
# (2) Remove buffer + kill loop #
on_exit(){
echo " < closing binding port $IN_PT to $BUFFER";
rm $BUFFER 2>/dev/null && echo " < $BUFFER deleted";
[[ $DEBUG ]] && echo " < closing binding port $IN_PT to $BUFFER";
rm $BUFFER 2>/dev/null && ( [[ $DEBUG ]] && echo " < $BUFFER deleted" );
kill -INT $LOOP_PID 2>/dev/null;

View File

@ -7,7 +7,7 @@
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;
test $# -lt 3 && 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;
@ -19,6 +19,10 @@ OUT_ID="$1";
OUT_IP="$2";
OUT_PT="$3";
# (5) Manage optional 'silent' argument #
DEBUG="";
test $# -ge 4 -a "$4" == "debug" && DEBUG="xx";
# (2) Prepare buffer
@ -27,8 +31,8 @@ OUT_PT="$3";
BUFFER="/tmp/outbuf_$OUT_ID";
# (2) Create/Flush buffer
test -f $BUFFER && rm $BUFFER;
touch $BUFFER;
# test -f $BUFFER && rm $BUFFER;
echo -n "" > $BUFFER;
@ -36,7 +40,7 @@ touch $BUFFER;
# (3) Launch OUTPUT server
#--------------------------------------------------------#
(
echo " > binding $BUFFER to $OUT_IP:$OUT_PT";
[[ $DEBUG ]] && echo " > binding $BUFFER to $OUT_IP:$OUT_PT";
trap "kill -9 $NC_PID 2>/dev/null; exit 1;" HUP INT KILL TERM;
outfail=0;
@ -44,9 +48,6 @@ touch $BUFFER;
# (1) kill script after 10 failures
while [ $outfail -lt 10 ]; do
# (2) bind socket to buffer
# echo " + $BUFFER -> $OUT_IP:$OUT_PT";
# (3) Check if port open
nc -z $OUT_IP $OUT_PT >/dev/null 2>&1;
PORT_CLOSED=$?;
@ -73,8 +74,8 @@ LOOP_PID=$!;
# (2) Remove buffer + kill loop #
on_exit(){
echo " < closing binding from $BUFFER to $OUT_IP:$OUT_PT";
rm $BUFFER 2>/dev/null && echo " < $BUFFER deleted";
[[ $DEBUG ]] && echo " < closing binding from $BUFFER to $OUT_IP:$OUT_PT";
rm $BUFFER 2>/dev/null && ( [[ $DEBUG ]] && echo " < $BUFFER deleted" );
kill -INT $LOOP_PID 2>/dev/null;