dimanche, octobre 14, 2007

/dev/tcp ou netcat ?

En réaction au blog de bodman, qui nous présentait une manière élégante (tirée d'un autre blog: unixjunkie) capable de lancer des requêtes HTTP armé du seul shell "BASH":

exec 3<> /dev/tcp/www.google.com/80
printf "GET / HTTP/1.0\n\n" >&5
cat <&5
exec 5>&-


qui se soldait sur mon ubuntu invariablement par un "bash: /dev/tcp/www.google.com/80: No such file or directory". Intrigué, j'ai un peu cherché sur les forums, puis j'ai téléchargé les sources, et dans le README, je trouve ceci:

9. Why is bash configured with --disable-net-redirections?

It can produce completely unexpected results. This kind of
feature should not be part of a shell but a special. tool. And
that tool has existed for years already, it's called netcat.



aha. man netcat, alors ...


netcat is a simple unix utility which reads and writes data across net-
work connections, using TCP or UDP protocol. It is designed to be a
reliable "back-end" tool that can be used directly or easily driven by
other programs and scripts.
In the simplest usage, "nc host port" creates a TCP connection to the
given port on the given target host. Your standard input is then sent
to the host, and anything that comes back across the connection is sent
to your standard output.


ma réponse de TCSHeur sera donc:
debian> echo "get / http/1.0\n\n" | nc www.google.com 80


Je vous laisse juge ^_^

3 commentaires:

bodman a dit…

Ce petit outil semble très pratique :)
A savoir pour les basheurs, on utilisera printf au lieu de echo car la substitution du \n semble difficile :
printf "GET / HTTP/1.0\n\n" | nc www.google.com 80

ScriptFanix a dit…

echo -e "GET / HTTP/1.0\n\n" \
| nc www.google.com 80

sylvainulg a dit…

thanks for the comment, scriptfanix. I was wondering why all those echo -e "stuff" were displaying "-e stuff" on my computer.

It just appeared that TCSH here does not use /bin/echo at all, but that it instead consider echo to be a built-in command... so when *i* type 'echo "\a"' here, it beeps. If i try the same in bash, it doesn't.

:P