使用 netcat 编写 HTTP 标头请求脚本
我正在尝试使用 netcat 来了解有关 HTTP 工作原理的更多信息。 我想用 bash 或 Perl 编写其中一些脚本,但我在测试的早期就遇到了障碍。
如果我直接从提示符处运行 netcat 并输入 HEAD 请求,它就会起作用,并且我会收到我正在探测的 Web 服务器的标头。
这是有效的:
[romandas@localhost ~]$ nc 10.1.1.2 80 HEAD / HTTP/1.0 HTTP/1.1 200 OK MIME-Version: 1.0 Server: Edited out Content-length: 0 Cache-Control: public Expires: Sat, 01 Jan 2050 18:00:00 GMT [romandas@localhost ~]$
但是当我将相同的信息放入文本文件并通过管道或重定向将其提供给 netcat 准备脚本时,它不会返回标头。
该文本文件由 HEAD 请求和两个换行符组成:
HEAD / HTTP/1.0
通过 echo 或 printf 发送相同的信息也不起作用。
$ printf "HEAD / HTTP/1.0\r\n"; |nc -n 10.1.1.2 80 $ /bin/echo -ne 'HEAD / HTTP/1.0\n\n' |nc 10.1.1.2 80
有什么想法我做错了吗? 不确定是 bash 问题、echo 问题还是 netcat 问题。
我通过 Wireshark 检查了流量,成功的请求(手动键入)在第二个数据包中发送尾随换行符,而 echo、printf 和文本文件方法将换行符保留在同一个数据包中,但我不确定原因是什么这种行为。
I'm trying to play around with netcat to learn more about how HTTP works. I'd like to script some of it in bash or Perl, but I've hit upon a stumbling block early on in my testing.
If I run netcat straight from the prompt and type in a HEAD request, it works and I receive the headers for the web server I'm probing.
This works:
[romandas@localhost ~]$ nc 10.1.1.2 80 HEAD / HTTP/1.0 HTTP/1.1 200 OK MIME-Version: 1.0 Server: Edited out Content-length: 0 Cache-Control: public Expires: Sat, 01 Jan 2050 18:00:00 GMT [romandas@localhost ~]$
But when I put the same information into a text file and feed it to netcat through a pipe or via redirection, in preparation for scripting, it doesn't return the headers.
The text file consists of the HEAD request and two newlines:
HEAD / HTTP/1.0
Sending the same information via echo or printf doesn't work either.
$ printf "HEAD / HTTP/1.0\r\n"; |nc -n 10.1.1.2 80 $ /bin/echo -ne 'HEAD / HTTP/1.0\n\n' |nc 10.1.1.2 80
Any ideas what I'm doing wrong? Not sure if it's a bash problem, an echo problem, or a netcat problem.
I checked the traffic via Wireshark, and the successful request (manually typed) sends the trailing newline in a second packet, whereas the echo, printf, and text file methods keep the newline in the same packet, but I'm not sure what causes this behavior.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要两对“\r\n”,并告诉
netcat
等待响应。以下或类似的内容应该有效:
You need two pairs of "\r\n", and also to tell
netcat
to wait for a response.The following or something similar should work:
另一种方法是使用所谓的“heredoc”约定。
Another way is to use what is called the 'heredoc' convention.
让 nc 等待响应的另一种方法是向输入添加睡眠。 例如
Another way to get nc to wait for the response is to add a sleep to the input. e.g.
您可以使用下面的 netcat 命令来创建您的实例网络服务器:
You can use below netcat command to make your instance webserver:
该行也将起到等效作用:
This line will also work as equivalent: