Subject: bash's /dev/tcp/hostname/port

From: Muli Ben-Yehuda <[email-masked]>
Date: Thu, 6 Jun 2002 11:12:17 +0300

While perusing the bash manual
(http://www.gnu.org/manual/bash/html_mono/bashref.html#SEC37), I came
accross a very nice tidbit. Allow me to demonstrate: 

In one terminal: 
mulix@tea:~$ nc -l -p 31337 # set up a listening socket

In the other:
 
mulix@tea:~$ cat > /dev/tcp/localhost/31337
bash rules!
mulix@tea:~$ 

And in the first terminal, we get: 
mulix@tea:~$ nc -l -p 31337
bash rules!
mulix@tea:~$ 

bash knows how to do redirection to/from a tcp (and also udp) socket!
bash version is 2.05a.0(1)-release.

I'm now waiting for Nadav to let us all know that zsh also does ssl
encryption on socket redirection, and has been doing it since unix
hackers were still too young to grow beards. 
-- 
Monday 15 Forelithe 7466

http://vipe.technion.ac.il/~mulix/
http://syscalltrack.sf.net/

---

From: "Nadav Har'El" <[email-masked]>
Date: Thu, 6 Jun 2002 11:23:16 +0300

> mulix@tea:~$ cat > /dev/tcp/localhost/31337

Eeek, the old Awk "I have a license to mess around with filenames beginning
with /dev" trick :(

> hackers were still too young to grow beards. 

I'm not aware of such a feature in zsh, but I wouldn't be surprised if
there is (the latest zsh even has a dynamically-loadable module for a
builtin ftp client! see zshmodules(1)).

Bu I don't understand why not use nc for this direction to - instead of
"cat > /dev/tcp/lcalhost/31337", you can do "nc localhost 31337".

-- 
Nadav Har'El                        |      Thursday, Jun 6 2002, 26 Sivan 5762
[email-masked]             |-----------------------------------------
Phone: +972-53-245868, ICQ 13349191 |New! Divorcee Barbie! Comes with all the
http://nadav.harel.org.il           |usual accessories, plus all of Ken's stuff

---

From: Muli Ben-Yehuda <[email-masked]>
Date: Thu, 6 Jun 2002 11:25:27 +0300

> builtin ftp client! see zshmodules(1)).

Does it have a builtin mail command? 

> "cat > /dev/tcp/lcalhost/31337", you can do "nc localhost 31337".

It's another tool in one's toolchest, that is all. A neat one,
too. Not every machine has nc on it, unfortunately, while almost any
machine will have bash. 
-- 
Monday 15 Forelithe 7466

---

From: Adi Stav <[email-masked]>
Date: Sun, 9 Jun 2002 12:58:28 +0300

Yay! Shell flamewar!

> Eeek, the old Awk "I have a license to mess around with filenames beginning

I agree. Great feature, ugly syntax. I'd prefer special syntax that 
resembles the (in itself admittedly ugly) socket API. E.g., 

cat > connect tcp:localhost:31337

or something of that sort. Then you can do stuff like 

while exec < accept &3
do
       cat
done 3<> listen tcp

Syntactic incompatility with zsh's multiple redirection is an added bonus.

By the way, there's no problem writing fully featured servers even 
in Bourne. Just plop the script into /etc/inetd.conf .

> builtin ftp client! see zshmodules(1)).

I will demonstrate self-restraint.

> "cat > /dev/tcp/lcalhost/31337", you can do "nc localhost 31337".

The semantics are different. With:

cat | nc localhost 31337

cat has the write end of a pipe open on file descriptor 1. The reader
just happens to send it over the network. With:

cat > /dev/tcp/localhost/31337

cat has a TCP socket open on file descriptor 1. At the very least it
can run getsockname() on it, and the like. It's like saying that there's
no need for redirection in shells, because you can write an appropriate
utility and do:

cat | writeto some-file-name

which is just not the same thing.