Linux script telnet HEAD request [duplicate]

被刻印的时光 ゝ 提交于 2019-12-08 04:26:23

问题


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.

This is the script I made:

echo "telnet $1 $2"
sleep 10
echo "HEAD $3 HTTP/1.0"
echo
echo
sleep 2

Typing this in the console:

./gethost microsoft.com 80 /

Give this as a result:

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).


回答1:


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'

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.

See man bash for a description of the <<< operator.




回答2:


You aren't actually running the telnet command anywhere. You just echo it to the screen.

Drop the echo from that line.

That being said that won't work either because you don't get an interactive telnet session in a script that way.

You might be able to pipe input to telnet to give it strings to send to the remove host but I'm not sure.

You can use something like netcat/nc instead of telnet for this (which is likely a good idea anyway) as they definitely can take input via a pipe.

You could also just use a tool built for this like curl (curl -I).



来源:https://stackoverflow.com/questions/26850271/linux-script-telnet-head-request

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!