views:

177

answers:

3

Hi

I would like to script telnet to test my website inputs handling. I can do it manually :

telnet localhost 8888
...
GET / HTTP/1.1\n
Host: localhost


...html response

But I can pass command to telnet in my shell script !

I've tried :

(echo "GET / HTTP/1.1\n"; echo "Host: localhost \n\n"; sleep 1) | telnet localhost 8888

It produces no results at all !

+2  A: 

(Probably not the only issue) You should "echo -en", and use single quote to close strings. This makes bash interpret \n correctly

Regards

Giuseppe Guerrini
+3  A: 

I find that netcat, aka nc, is much better suited to this sort of task.

Also as Giuseppe mentions you'll have to fix the quoting. An alternative to bash -e is to quote using $'this form', in which \n, \t, etc. are interpreted by bash when parsing the command line rather than being passed on to echo. So that form is applicable to many different utilities.

intuited
+1, nc is better suited for that IMHO.
David V.
Also, recent versions of bash recognize the "/dev/tcp/host/port" pseudofile. By using this feature (if available) telnet isn't necessary anymore. Bye
Giuseppe Guerrini
afaik, /dev/tcp is not a feature of bash, it's dependent on the kernel config, or enabled modules, or ..something. Anyway some distros (including ubuntu and I believe most debian-likes) don't have this facility.But I could be wrong.. is there a better way to check than `ls /dev/tcp`? This gives me "No such file or directory" under ubuntu 9.10.
intuited
It depends on the build. Here's what "man bash" says on my Ubuntu: Bash handles several filenames specially when they are used in redirections, as described in the following table:[...] /dev/tcp/host/port If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open a TCP connection to the corresponding socket. NOTE: Bash, as packaged for Debian, does not support using the /dev/tcp and /dev/udp files.
Giuseppe Guerrini
(continue) So it depends on the particular build. Anyway it's not a kernel feature. The Debian (and Ubuntu) one doesn't actually support it. Regards
Giuseppe Guerrini
weird. I always thought that was implemented with a device driver, but it's actually a bash issue. Also it looks like the bash that is current in Karmic (Ubuntu 9.10) is actually compiled with this feature. I stand corrected. More on the history of the issue at https://bugs.launchpad.net/ubuntu/+source/bash/+bug/215034
intuited
It's notable that this is just something that `bash` does, so other programs can't direct/get output/input to/from a `/dev/tcp` location. well at least not without going through `bash`.
intuited
A: 

The usual answer to "How do I script x?" is "expect"

Dennis Williamson