I am trying to write a simple bash script that listens on a port and responds with a trivial HTTP response. My particular problem is that I am not sure if the port is available and if the binding fails, I return to the next port until the binding is complete.
So far, for me the easiest way to achieve this has been:
for (( i=$PORT_BASE; i < $(($PORT_BASE+$PORT_RANGE)); i++ ))
do
if [ $DEBUG -eq 1 ] ; then
echo trying to bind on $i
fi
/usr/bin/faucet $i --out --daemon echo test 2>/dev/null
if [ $? -eq 0 ] ; then
port=$i
if [ $DEBUG -eq 1 ] ; then
echo "bound on port $port"
fi
break
fi
done
Here I use faucetfrom the netpipes package Ubuntu.
The problem is that if I just print a โtestโ on the output, it curlcomplains about a non-standard HTTP response (error code 18). This is fair enough since I am not printing an HTTP compatible response.
If I replaced echo testwith echo -ne "HTTP/1.0 200 OK\r\n\r\ntest", curl still complains:
user@server:$ faucet 10020 --out --daemon echo -ne "HTTP/1.0 200 OK\r\n\r\ntest"
...
user@client:$ curl ip.of.the.server:10020
curl: (56) Failure when receiving data from the peer
, , faucet . , netcat, curl :
user@server:$ echo -ne "HTTP/1.0 200 OK\r\n\r\ntest\r\n" | nc -l 10020
...
user@client:$ curl ip.of.the.server:10020
test
user@client:$
faucet netcat script, , , . faucet --daemon, , $? ( ), , bind. netcat , & $? .
- , faucet / . faucet, netcat, , bash ( , - , Perl Python).