绑定端口与netpipes / netcat的 [英] Binding on a port with netpipes/netcat
问题描述
我试图写监听端口上,并用琐碎的HTTP响应响应一个简单的bash脚本。我的具体问题是,我不知道该端口是否可用,在绑定失败的情况下,我回落到下一个端口,直到绑定成功。
到目前为止,我实现这一目标的最简单的方法是这样的:
为((I = $ PORT_BASE; I< $(($ PORT_BASE + $ PORT_RANGE));我+ +))
做
如果[$ DEBUG -eq 1];然后
回声试图绑定在$ I
科幻
在/ usr /斌/水龙头$ I --out --daemon echo测试2 - ;的/ dev / null的
如果[$? -eq 0];然后#SUCCESS?
端口= $ I
如果[$ DEBUG -eq 1];然后
回声绑定端口$端口
科幻
打破
科幻
DONE
在这里,我使用水龙头
来自的 netpipes Ubuntu的软件包。
这样做的问题是,如果我只是打印测试的输出,卷曲
抱怨非标准的HTTP响应(错误code 18)。这很公平,因为我不打印HTTP兼容响应。
如果我更换 echo测试
与回声-neHTTP / 1.0 200 OK \\ r \\ n \\ r \\ NTEST
,卷曲还是会抱怨:
用户名@服务器:$龙头10020 --out --daemon回声-neHTTP / 1.0 200 OK \\ r \\ n \\ r \\ NTEST
...
用户@客户:$卷曲ip.of.the.server:10020
卷曲:(56)失败时收到对端的数据
我认为问题在于如何水龙头
正在打印的响应和处理的连接。例如,如果我做服务器端的的netcat
,卷曲正常工作:
用户名@服务器:$回声-neHTTP / 1.0 200 OK \\ r \\ n \\ r \\ NTEST \\ r \\ n| NC -l 10020
...
用户@客户:$卷曲ip.of.the.server:10020
测试
用户@客户:$
我会更乐意来替换水龙头
与的netcat
在我的主脚本,但问题是,我想产卵独立服务器进程,以便能够从相同的基壳运行客户。 水龙头
有一个非常方便的 - 后台程序
参数,因为它叉为背景,我可以使用 $?
(退出状态code),以检查是否绑定成功。如果我是使用的netcat
类似用途,我会用它来叉&安培;
和 $?
是行不通的。
是否有人知道为什么水龙头
未正确在这个特殊的情况下,响应和/或可以提出一个解决这个问题。我没有结婚既不水龙头
也不的netcat
,但想使用bash实现的解决方案,或者它的实用程序(如反对写另一个脚本语言的东西,例如Perl或Python)。
龙头10020 --out --daemon \\
回声-neHTTP / 1.0 200 OK \\ r \\ nContent长度:4 \\ r \\ n \\ r \\ NTEST
正常工作。这个问题似乎是,回声
不知道如何正确关机
插座,只用关闭
,而是和卷曲
是不满从<$ C得到一个-1(无序关机),而不是0(有序地关闭) $ C> recvfrom的。
尝试 socat
这枝各地要清理孩子完成后。
socat TCP-L:10020,叉,reuseaddr \\
EXEC:回声-neHTTP / 1.0 200 OK \\ r \\ n \\ r \\ NTEST'
I am trying to write a simple bash script that is listening on a port and responding with a trivial HTTP response. My specific issue is that I am not sure if the port is available and in case of bind failure I fall back to next port until bind succeeds.
So far to me the easiest way to achieve this was something like:
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 #success?
port=$i
if [ $DEBUG -eq 1 ] ; then
echo "bound on port $port"
fi
break
fi
done
Here I am using faucet
from netpipes Ubuntu package.
The problem with this is that if I simply print "test" to the output, curl
complains about non-standard HTTP response (error code 18). That's fair enough as I don't print HTTP-compatible response.
If I replace echo test
with 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
I think the problem lies in how faucet
is printing the response and handling the connection. For example if I do the server side in netcat
, curl works fine:
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:$
I would be more than happy to replace faucet
with netcat
in my main script, but the problem is that I want to spawn independent server process to be able to run client from the same base shell. faucet
has a very handy --daemon
parameter as it forks to background and I can use $?
(exit status code) to check if bind succeeded. If I was to use netcat
for a similar purpose, I would have to fork it using &
and $?
would not work.
Does anybody know why faucet
isn't responding correctly in this particular case and/or can suggest a solution to this problem. I am not married neither to faucet
nor netcat
but would like the solution to be implemented using bash or it's utilities (as opposed to write something in yet another scripting language, such as Perl or Python).
faucet 10020 --out --daemon \
echo -ne "HTTP/1.0 200 OK\r\nContent-Length: 4\r\n\r\ntest"
works fine. The issue seems to be that echo
doesn't know how to properly shutdown
a socket, using just close
instead, and curl
is unhappy about getting a -1 (disorderly shutdown) rather than a 0 (orderly shutdown) from recvfrom
.
Try socat
which sticks around to clean up after the child is done.
socat tcp-l:10020,fork,reuseaddr \
exec:'echo -ne "HTTP/1.0 200 OK\r\n\r\ntest"'
这篇关于绑定端口与netpipes / netcat的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!