开发者

how to feed a file to telnet

开发者 https://www.devze.com 2022-12-26 16:22 出处:网络
trying to understand http and headers i was playing around with telnet to send requests. to not type everything again and again and again i thought i\'d write a small textfile with all the commands i

trying to understand http and headers i was playing around with telnet to send requests. to not type everything again and again and again i thought i'd write a small textfile with all the commands i need.

my file is as sim开发者_如何学Cple as follows:

GET /somefile.php HTTP/1.1
Host: localhost

i then try to feed it to telnet with io-redirection:

$ telnet localhost 80 < telnet.txt

but all output i get is

Trying ::1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

what am i doing wrong?


telnet is not the right tool for this job. Try :

 netcat localhost 80 < telnet.txt

btw, if you don't have the tool installed on your machine, you can get it here:

http://netcat.sourceforge.net/


The problem is that you feed all input to the telnet command instantly, without waiting for its output. Right after all of your input file has been fed, it will automatically cause telnet to "hang up", because the input stream reaches its end (EOF). Telnet might still be in its first millisecond or microsecond of actually waiting to establish a connection to the remote server when this happens. What you want to do is, send a command to telnet, then wait for it to do its job aka wait for the server's response, then send the next command, and so on, and only hang up at the very end when all the commands you sent were actually processed. To do this, use an 'expect' script instead of insta-feeding a text file. 'Expect' is the usual tool to do this job.


The advice above is pretty good, except the port number is 23, not 80, for telnet. So if you came here looking for how to feed data into a telnet port (as I did, to configure an ethernet-controlled power switch) then use port 23.


I don't know if that's possible with telnet. Have you looked at netcat?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号