diff --git a/bind-output b/bind-output index ff9ea6b..c62baef 100755 --- a/bind-output +++ b/bind-output @@ -41,12 +41,25 @@ touch $BUFFER; outfail=0; # (1) kill script after 10 failures -. while [ $outfail -lt 10 ]; do + while [ $outfail -lt 10 ]; do # (2) bind socket to buffer echo "(.) binding $BUFFER to $OUT_IP:$OUT_PT"; - ( tail -f $BUFFER | nc $OUT_IP $OUT_PT 2> /dev/null && outfail="0" || ( outfail=`expr $outfail + 1`; exit 1 ) || echo " * cannot access $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;