Network connection from the shell script.
Shell allow users to send udp or tcp messages to the remote host and to receive replies directly from the command line without any additional tools. It seems like a quite useful thing, but the functionality are limited to act only as a tcp/udp client, and not as a server.
The whole following code shows how to exploit this functionality. It should works properly in both major shell interpretors: Bash and Ksh.
exec 3<> /dev/tcp/$host_name/$port_number
echo -e "GET / HTTP/1.0\nHost: $host_name\n\n" >&3
while true ;do
read -r response <&3
if [ -z "$response" ];then
Here we are opening a new file descriptor with number 3 by using i/o redirection in a special format, that starts as a /dev/… and contain a protocol definition, a host name and a port number. To not to stuck in a reading process for waiting until connection would not be closed by the server (due to KeepAlive ) HTTP/1.0 is used instead of HTTP/1.1 .
As a result of execution, script will display something like this:
HTTP/1.1 200 OK
Date: Thu, 31 Mar 2011 16:11:13 GMT
Last-Modified: Wed, 02 Mar 2011 14:00:03 GMT
-- Some HTML code --