The UNIX way.

Around the clock, across the globe. By Vladimir Legeza

Network connection from the shell script.

leave a comment »

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.

host_name=localhost
port_number=80

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
echo $response
if [ -z "$response" ];then
break
fi
done

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_request.sh
HTTP/1.1 200 OK
Server: nginx/0.7.67
Date: Thu, 31 Mar 2011 16:11:13 GMT
Content-Type: text/html
Content-Length: 177
Last-Modified: Wed, 02 Mar 2011 14:00:03 GMT
Connection: close
Accept-Ranges: bytes

....
-- Some HTML code --
....

Advertisements

Written by Vladimir Legeza

March 31, 2011 at 4:23 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: