diff --git a/bind-input b/bind-input index c250d81..296f42d 100755 --- a/bind-input +++ b/bind-input @@ -36,6 +36,8 @@ touch $BUFFER; #--------------------------------------------------------# ( + echo " > binding port $IN_PT to $BUFFER"; + trap "kill -9 $NC_PID 2>/dev/null" INT KILL; infail=0; @@ -43,7 +45,7 @@ touch $BUFFER; while [ $infail -lt 10 ]; do # (2) bind socket to buffer - echo "(.) binding port $IN_PT to $BUFFER"; + # echo " + $IN_PT -> $BUFFER"; # (3) Check if port already in use ss -tl4 "( sport = :$IN_PT )" | grep "$IN_PT" >/dev/null; @@ -52,7 +54,8 @@ touch $BUFFER; # (4) Manage taken port # if [ "$PORT_TAKEN" -ne "1" ]; then infail="`expr $infail + 1`"; - sleep .5; + + test "$infail" -gt "1" && sleep .5; continue; fi; @@ -69,6 +72,13 @@ LOOP_PID=$!; # (2) Remove buffer + kill loop # -trap "rm $BUFFER 2>/dev/null; kill -9 $LOOP_PID 2>/dev/null" INT KILL; +on_exit(){ + echo " < closing binding port $IN_PT to $BUFFER"; + rm $BUFFER 2>/dev/null; + kill -9 $LOOP_PID 2>/dev/null; +} +trap "on_exit;" INT KILL; -wait $LOOP_PID; \ No newline at end of file +wait $LOOP_PID; + +on_exit; \ No newline at end of file diff --git a/bind-output b/bind-output index c62baef..46c1d80 100755 --- a/bind-output +++ b/bind-output @@ -33,9 +33,10 @@ touch $BUFFER; -# (3) Launch INPUT server +# (3) Launch OUTPUT server #--------------------------------------------------------# ( + echo " > binding $BUFFER to $OUT_IP:$OUT_PT"; trap "kill -9 %1 2>/dev/null" INT KILL; outfail=0; @@ -44,22 +45,23 @@ touch $BUFFER; while [ $outfail -lt 10 ]; do # (2) bind socket to buffer - echo "(.) binding $BUFFER to $OUT_IP:$OUT_PT"; + # echo " + $BUFFER -> $OUT_IP:$OUT_PT"; # (3) Check if port open - nc $OUT_IP $OUT_PT < /dev/null; + nc -z $OUT_IP $OUT_PT >/dev/null 2>&1; PORT_CLOSED=$?; # (4) Manage taken port # if [ "$PORT_CLOSED" -eq "1" ]; then outfail="`expr $outfail + 1`"; - sleep .5; + + test "$outfail" -gt "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" )& + ( tail -f $BUFFER | nc $OUT_IP $OUT_PT 2> /dev/null )& wait $!; done; @@ -69,6 +71,13 @@ LOOP_PID=$!; # (2) Remove buffer + kill loop # -trap "rm $BUFFER 2>/dev/null; kill -9 $LOOP_PID 2>/dev/null" INT KILL; +on_exit(){ + echo " < closing binding from $BUFFER to $OUT_IP:$OUT_PT"; + rm $BUFFER 2>/dev/null; + kill -9 $LOOP_PID 2>/dev/null; +} +trap "on_exit;" INT KILL; -wait $LOOP_PID; \ No newline at end of file +wait $LOOP_PID; + +on_exit; \ No newline at end of file