From 6c5d87d2ba239424f6127d0ff0c51e668d590773 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 30 Jan 2018 18:46:00 +0100 Subject: [PATCH] fix: actual 'failures' management (.5s between each failure + stop after 10 consecutive ones) + on success do as usual --- bind-input | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/bind-input b/bind-input index 15dfac5..c250d81 100755 --- a/bind-input +++ b/bind-input @@ -10,8 +10,8 @@ ROOT=$(dirname `realpath $0`); test $# -lt 2 && echo -e "error: too fewarguments\n\n\e[1mUSAGE\e[0m\n\tbind-input \n\n\e[1mARGUMENTS\e[0m\n\t\tThe identifier of the input (buffer name)\n\t\tTo port to listen to\n" && exit 1; # (3) Check @PORT range # -MIN_PORT=49152; -MAX_PORT=65535; +MIN_PORT=1024; +MAX_PORT=49151; test "$2" -gt "$MAX_PORT" -o "$2" -lt "$MIN_PORT" && echo "error: must be between $MIN_PORT and $MAX_PORT" && exit 1; # (4) Set argument explicit names # @@ -36,19 +36,31 @@ touch $BUFFER; #--------------------------------------------------------# ( - trap "kill -9 %1 2>/dev/null" INT KILL; + trap "kill -9 $NC_PID 2>/dev/null" INT KILL; infail=0; # (1) kill script after 10 failures while [ $infail -lt 10 ]; do - echo "failures: $infail"; - # (2) bind socket to buffer - echo "(.) binding :$IN_PT to $BUFFER"; + echo "(.) binding port $IN_PT to $BUFFER"; - ( nc -lp $IN_PT >> $BUFFER 2> /dev/null && infail="0" || ( infail=`expr $infail + 1`; exit 1 ) || echo " * cannot access :$IN_PT" )& - wait $!; + # (3) Check if port already in use + ss -tl4 "( sport = :$IN_PT )" | grep "$IN_PT" >/dev/null; + PORT_TAKEN=$?; + + # (4) Manage taken port # + if [ "$PORT_TAKEN" -ne "1" ]; then + infail="`expr $infail + 1`"; + sleep .5; + continue; + fi; + + # (5) Bind input + infail="0"; + ( nc -lp $IN_PT >> $BUFFER 2>/dev/null )& + NC_PID=$!; + wait $NC_PID; done;