Linux脚本telnet HEAD请求 [英] Linux script telnet HEAD request
问题描述
因此,基本上,我正在尝试做一些相当简单的事情;我试图制作一个脚本以telnet到Microsoft服务器并请求页面的HEAD.但是,正如预期的那样,它不起作用.如果我只是将其手动输入到控制台中,它将起作用,但是执行脚本时根本不起作用.
So basically I'm trying to do something fairly easy; I'm trying to make a script to telnet to the microsoft server and requesting the HEAD of the page. But as expected, it doesn't work. If I just enter it manually into the console it works, but when executing the script doesn't work at all.
这是我制作的脚本:
回显"telnet $ 1 $ 2"
睡觉10
回声"HEAD $ 3 HTTP/1.0"
回声
回声
睡觉2
echo "telnet $1 $2"
sleep 10
echo "HEAD $3 HTTP/1.0"
echo
echo
sleep 2
在控制台中输入:
./gethost microsoft.com 80/
./gethost microsoft.com 80 /
给出以下结果:
telnet microsoft.com 80
HEAD/HTTP/1.0
telnet microsoft.com 80
HEAD / HTTP/1.0
然后在最后两个空回显后返回控制台,老实说我听不到,甚至还增加了睡眠(可能存在网络延迟).
And then just returns to the console after the last two empty echos, I honestly don't get it, I even increased the sleeps (for possible network delays).
推荐答案
您的脚本仅向stdout打印一些内容.它实际上并没有执行 telnet
命令.尝试这样的事情:
Your script just prints some stuff to stdout. It doesn't actually execute the telnet
command. Try something like this:
telnet $1 $2 <<< $'HEAD $3 HTTP/1.0\r\n\r\n'
这将运行 telnet
,而不仅仅是输出命令.它还在stdin上提供 HEAD
命令.发送 \ r \ n
行尾而不是UNIX的默认 \ n
行尾很重要,因为 \ r \ n
是HTTP协议要求.
This will run telnet
instead of just printing the command out. It also feeds in the HEAD
command on stdin. It's important to send \r\n
line endings rather than UNIX's default \n
line endings, since \r\n
is what the HTTP protocol requires.
有关<<<
运算符的说明,请参见 man bash
.
See man bash
for a description of the <<<
operator.
这篇关于Linux脚本telnet HEAD请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!