16

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 simple 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?

knittl
  • 246,190
  • 53
  • 318
  • 364
  • 1
    Revisiting this question after 7 years, I think the problem with the proposed solutions with netcat were CRLF/LF issues with the file. HTTP protocol requires the use of CRLF line terminators and will not work with LF only. – knittl Dec 11 '17 at 18:55

4 Answers4

21

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/

user987339
  • 10,519
  • 8
  • 40
  • 45
chetan
  • 933
  • 4
  • 11
  • how can i get output back? i need the http/html response – knittl Apr 14 '10 at 17:51
  • The response will just show up on the stdout of the commandline. – chetan Apr 14 '10 at 18:16
  • it does when i enter the request on stdin, but when doing `nc < file` it will not display anything (although the request seems to have worked) – knittl Apr 14 '10 at 18:19
  • 6
    `cat telnet.txt - | nc localhost 80` will do the trick. cat sends the file, then stdin, to netcat. – Andreas Wederbrand Jan 15 '16 at 09:13
  • In my case I only have telnet available and cannot install nc due to security restrictions. I also created a text file, but rather than passing the file to telnet, I copied the contents into the terminal clipboard buffer and then pasted them after the telnet connection was established. – shonky linux user Feb 14 '17 at 01:51
  • For any lost soul finding this question and answer: HTTP protocol requires CRLF line terminators and will not work with LF only. – knittl Dec 11 '17 at 18:55
  • 2
    telnet IS the right tool for this job if you use the syntax shared by @AndreasWederbrand: `cat telnet.txt - | telnet localhost 80` – baptx Dec 27 '17 at 15:03
7

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.

user2316370
  • 263
  • 1
  • 4
  • 11
1

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.

0

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

amertune
  • 660
  • 5
  • 9