Feeding contents of a text file as command to telnet

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.


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