Feeding contents of a text file as command to telnet

27,824

Solution 1

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.

Solution 2

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
Share:
27,824

Related videos on Youtube

Bentley4
Author by

Bentley4

Profile with mostly old questions where I learned about programming. Thank you SO!

Updated on September 18, 2022

Comments

  • Bentley4
    Bentley4 almost 2 years

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

    • terdon
      terdon over 10 years
      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?
    • Bentley4
      Bentley4 over 10 years
      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
      Bentley4 over 10 years
      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
      Bentley4 over 10 years
      @ 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.
    • terdon
      terdon over 10 years
      Dammit, theoretically, you should be able to do telnet < request.txt but I can't get the GET command to work.
    • voices
      voices about 8 years
      @Bentley4 You actually type <ENTER> ?
  • Bentley4
    Bentley4 over 10 years
    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.
  • terdon
    terdon over 10 years
    @Bentley4 no problem, using nc is the better solution, you should accept it.
  • davidgo
    davidgo over 10 years
    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.
  • 2rs2ts
    2rs2ts over 8 years
    I fail to see how interact is the right way to end that expect script.
  • terdon
    terdon over 8 years
    @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.
  • 2rs2ts
    2rs2ts over 8 years
    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.)
  • terdon
    terdon over 8 years
    @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.
  • 2rs2ts
    2rs2ts over 8 years
    Yeah, that's the thing, I actually couldn't get out with the EOF. Can't remember if I tried exit, probably not.
  • terdon
    terdon over 8 years
    @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.