bash 使用 netcat 编写 HTTP 标头请求的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/642707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scripting an HTTP header request with netcat
提问by romandas
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.
我正在尝试使用 netcat 以了解有关 HTTP 工作原理的更多信息。我想用 bash 或 Perl 编写其中的一些脚本,但我在测试的早期就遇到了一个绊脚石。
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.
如果我直接从提示符运行 netcat 并输入 HEAD 请求,它就会工作,并且我会收到我正在探测的 Web 服务器的标头。
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:
但是,当我将相同的信息放入文本文件并通过管道或重定向将其提供给 netcat 以准备编写脚本时,它不会返回标头。
文本文件由 HEAD 请求和两个换行符组成:
HEAD / HTTP/1.0
Sending the same information via echoor printfdoesn't work either.
通过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
Any ideas what I'm doing wrong? Not sure if it's a bash problem, an echo problem, or a netcat problem.
任何想法我做错了什么?不确定是 bash 问题、echo 问题还是 netcat 问题。
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.
我通过 Wireshark 检查了流量,成功的请求(手动输入)在第二个数据包中发送尾随换行符,而 echo、printf 和文本文件方法将换行符保留在同一个数据包中,但我不确定是什么原因造成的这种行为。
回答by moonshadow
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.
您需要两批“\r\n”,并告诉 netcat 等待响应。printf "HEAD / HTTP/1.0\r\n\r\n" |nc -n -i 1 10.1.1.2 80
或类似的应该工作。
回答by Bruce S
Another way is to use what is called the 'heredoc' convention.
另一种方法是使用所谓的“heredoc”约定。
$ nc -n -i 1 10.1.1.2 80 <<EOF
> HEAD / HTTP/1.0
>
> EOF
回答by Jay Soffian
Another way to get nc to wait for the response is to add a sleep to the input. e.g.
让 nc 等待响应的另一种方法是在输入中添加睡眠。例如
(printf 'GET / HTTP/1.0\r\n\r\n'; sleep 1) | nc HOST 80
回答by Prashant Shetty
You can use below netcat command to make your instance webserver:
您可以使用以下 netcat 命令来制作您的实例网络服务器:
MYIP=$(ifconfig eth0|grep 'inet addr'|awk -F: '{print }'| awk '{print }')
while true; do echo -e "HTTP/1.0 200 OK\r\n\r\nWelcome to $MYIP" | sudo nc -l -p 80 ; done&
回答by Deming
This line will also work as equivalent:
此行也将等效:
echo -e "HEAD / HTTP/1.1\nHost: 10.1.1.2\nConnection: close\n\n\n\n" | netcat 10.1.1.2 80