将文本文件的内容作为命令提供给 telnet

Ben*_*ey4 12 linux telnet shell http webserver

使用该命令telnet docs.python.org 80,我可以http://docs.python.org/2/license.html通过键入实际请求对 执行手动 HTTP请求。

现在,我想从文本文件中输入请求,而不是实时输入。

我试过这个:

cat request.txt|telnet docs.python.org 80


请求.txt

GET /2/license.html HTTP/1.1 
Host: docs.python.org
Run Code Online (Sandbox Code Playgroud)

(你必须用一个空行结束文件,否则你会得到一个错误的请求!)


但是与服务器的连接立即关闭。

我应该如何正确地通过管道request.txttelnet docs.python.org 80


编辑:

很高兴知道;如果使用HEAD代替,除了消息正文之外GET,您将获得与执行GET请求相同的响应。
因此,HEAD如果您只想检查 HTTP 标头,请使用。(即,响应的内容不会使您的 shell 输出混乱。)

dav*_*dgo 25

使用 netcat(nc 命令)而不是“telnet”,所以

猫请求.txt | nc docs.python.org 80

Telnet 是一种快速简便的 hack,但 netcat 显然是完成这项工作的正确工具。


ter*_*don 6

我真的没有任何经验,telnet但它确实从文件重定向中获取输入:

telnet < abc.txt
Run Code Online (Sandbox Code Playgroud)

我可以让它正确连接到服务器,如下所示:

$ cat abc.txt
open docs.python.org 80
$ telnet < abc.txt
telnet> Trying 82.94.164.162...
Connected to dinsdale.python.org.
Escape character is '^]'.
Connection closed by foreign host.
Run Code Online (Sandbox Code Playgroud)

也许你可以弄清楚如何让它接受GET命令,但我不能。另一种方法是使用expect脚本:

#!/usr/bin/expect

spawn telnet docs.python.org 80
expect "Escape character is '^]'." { 
     send "GET /2/license.html HTTP/1.1\nHost: docs.python.org\n\n" 
}
interact
Run Code Online (Sandbox Code Playgroud)

然后,您可以将脚本另存为telnet.exp,使其可执行并运行它:

./telnet.exp > output.html
Run Code Online (Sandbox Code Playgroud)