From 534b5a97acb3da14cf0cf3e0ce027f0852723862 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 1 Feb 2018 00:21:34 +0100 Subject: [PATCH] updated bind-input/output to be silent by default (can be activated with optional last param: 'debug') --- bind-input | 18 ++++++++++-------- bind-output | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/bind-input b/bind-input index 8379c41..2295d35 100755 --- a/bind-input +++ b/bind-input @@ -18,6 +18,11 @@ test "$2" -gt "$MAX_PORT" -o "$2" -lt "$MIN_PORT" && echo "error: 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; diff --git a/bind-output b/bind-output index 2ee442b..26ebaae 100755 --- a/bind-output +++ b/bind-output @@ -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 \n\n\e[1mARGUMENTS\e[0m\n\t\tThe identifier of the output (buffer name)\n\t\tThe ip or host of the target\n\t\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 \n\n\e[1mARGUMENTS\e[0m\n\t\tThe identifier of the output (buffer name)\n\t\tThe ip or host of the target\n\t\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;