与编写脚本的netcat的HTTP头请求 [英] Scripting an HTTP header request with netcat
问题描述
我试图玩弄netcat来了解更多关于如何HTTP的工作原理。我想脚本一些它在bash或Perl,但我已经在一个绊脚石早就在我的测试击中。
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.
如果我在一个HEAD请求作出了迅速和类型的netcat跑直线,它的工作原理和我收到的标头,我探测Web服务器。
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.
本作品:
[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的通过管道或通过重定向,在preparation的脚本,它不会返回头。结果
该文本文件由HEAD请求和两个新行:
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
通过发送相同的信息回声或的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的问题,回声等问题,或netcat的问题。
Any ideas what I'm doing wrong? Not sure if it's a bash problem, an echo problem, or a netcat problem.
我通过检查Wireshark的交通,并成功申请(手动输入)发送尾随的换行符在第二个数据包,而回声,printf的,和文本文件的方法保持在同一个包中的换行,但我不确定是什么原因导致这种行为。
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.
推荐答案
您需要两批为\\ r \\ n,并且还告诉netcat来等待响应。 printf的HEAD / HTTP / 1.0 \\ r \\ n \\ r \\ n|。NC -n -i 1 10.1.1.2 80
或类似的工作,应
You need two lots of "\r\n", and also to tell netcat to wait for a response. printf "HEAD / HTTP/1.0\r\n\r\n" |nc -n -i 1 10.1.1.2 80
or similar should work.
这篇关于与编写脚本的netcat的HTTP头请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!