Feeding contents of a text file as command to telnet

10

2

With the command telnet docs.python.org 80, I can do a manual HTTP request to http://docs.python.org/2/license.html, by typing the actual request.

Now, instead of typing it in live, I'd like to feed the request from a text file.

I tried this:

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


request.txt:

GET /2/license.html HTTP/1.1 
Host: docs.python.org

(You have to finish the file with a blank line or you'll get a bad request!)


But the connection to the server is closed immediately.

How should I properly pipe request.txt to telnet docs.python.org 80?


edit:

It's good to know; if you use HEAD instead of GET, you will get the same response as if you did a GET request, except for the message body.
So, use HEAD if you just want to examine the HTTP headers. (i.e. So that the contents of the response doesn't clutter your shell output.)

Bentley4

Posted 2013-11-07T12:50:29.707

Reputation: 1 588

@Bentley4 You actually type <ENTER> ? – voices – 2016-05-31T08:59:34.337

Could you include the actual command you run manually so we can compare? When I run the GET you have posted, I get a 408 Request Time-out error. Also, is wget http://docs.python.org/2/license.html not an option? – terdon – 2013-11-07T14:21:06.307

The commands are correct. I think you are making a connection with the server the moment you enter telnet docs.python.org 80, so you 'll have to hurry and type in those two lines(copy paste if necc.) within a few seconds or the server will return a time-out error. – Bentley4 – 2013-11-07T17:09:22.773

These are the exact commands: telnet docs.python.org 80 <ENTER> GET /2/license.html HTTP/1.1 <ENTER> Host: docs.python.org <ENTER> <ENTER> – Bentley4 – 2013-11-07T17:10:31.873

1

I think the reason why you need to type <ENTER> a second time at the end is because the HTTP protocol requires an extra empty line after the request headers. See the request message section of the HTTP wiki article

– Bentley4 – 2013-11-07T17:14:52.880

@ the wget solution: I want to define my request manually, with headers and all. That line in wget you proposed defines headers and message body automatically. – Bentley4 – 2013-11-07T17:19:30.733

1Dammit, theoretically, you should be able to do telnet < request.txt but I can't get the GET command to work. – terdon – 2013-11-07T17:24:44.753

Answers

21

Use netcat (nc command) rather then "telnet", so

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

Telnet is a quick and easy hack, but netcat is, apparently, the correct tool for the job.

davidgo

Posted 2013-11-07T12:50:29.707

Reputation: 49 152

How is Telnet 'quicker' and 'easier' then netcap? I don't see any usage difference in that regard for this case. – Bentley4 – 2013-11-07T19:09:13.247

Certainly not for this case, but I think people (read Me, and apparently you !!!) tend to use telnet because its the first thing we learnt, and, of-course, its available on Windows and Linux, where I believe netcat does not come pre-installed under Windows. – davidgo – 2013-11-08T00:11:16.380

5

I don't really have any experience with telnet but it does take input from file redirection:

telnet < abc.txt

I can get it to connect to the server correctly as follows:

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

Perhaps you can figure out how to get it to accept the GET command but I couldn't. An alternative is to use an expect script:

#!/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

You can then save the script as telnet.exp,make it executable and run it:

./telnet.exp > output.html

terdon

Posted 2013-11-07T12:50:29.707

Reputation: 45 216

I fail to see how interact is the right way to end that expect script. – 2rs2ts – 2015-10-13T21:30:20.547

@2rs2ts I know very little about expect, you might be right but I can't tell since your comment didn't tell me why you think it's wrong. My understanding is that interact "gives control of the current process to the user, so that keystrokes are sent to the current process, and the stdout and stderr of the current process are returned" (that's from man expect). So, interact is the way to give the user a way of actually using the telnet connection. – terdon – 2015-10-13T21:36:22.073

Since the original question was about feeding the contents of a file into telnet rather than interacting with it I think you should end with close -i $spawn_id or something like that. Then the script will exit. With interact I was actually unable to exit telnet at all, and had to close my terminal. Pretty miserable (although it may have been partially my fault.) – 2rs2ts – 2015-10-13T22:49:08.577

@2rs2ts ah, I see. Yes, that was intentional since I assumed the OP would want to interact with the telnet shell. Ctrl+D or exit should let you exit it. – terdon – 2015-10-13T22:52:14.283

Yeah, that's the thing, I actually couldn't get out with the EOF. Can't remember if I tried exit, probably not. – 2rs2ts – 2015-10-13T22:53:06.757

@2rs2ts OK, good to know. It's very late here so you m nor up for testing this but I'll try and check tomorrow. In the meantime, feel free to edit the answer and either change the code or add a warning or whatever you feel is best. Thanks for the heads up. – terdon – 2015-10-13T22:55:36.183

Cool, I didn't know of expect! I chose davidgo's answer because it is just the best solution for this case. I don't see any usage difference between netcap and telnet for what I wanted to achieve. Thanks for the effort nonetheless Terdon, if I could upvote your answer more, I would. – Bentley4 – 2013-11-07T19:23:34.317

1@Bentley4 no problem, using nc is the better solution, you should accept it. – terdon – 2013-11-07T20:01:01.287