TCP sockets in your shell

Usually I always used netcat to send TCP data but there’s another way, builtin in bash. You can send the string “Hello world” to on port 15123 using:

echo "Hello world" > /dev/tcp/

Simple, quick and effective :)

Note: thanks to Waldner for pointing out that it’s built-in in bash and there’s no need for an “mknode”


3 thoughts on “TCP sockets in your shell

  1. Let’s see if now I’m not caught by HTML formatting…

    You don’t need the special /dev/tcp file. “/dev/tcp” is actually a special device name that is intercepted and parsed directly by bash.

    See for example:

    $ ls /dev/tcp
    ls: /dev/tcp: No such file or directory
    $ exec 3<>/dev/tcp/
    $ echo "GET /" >&3
    $ cat <&3
    HTTP/1.0 302 Found
    Cache-Control: private
    Content-Type: text/html; charset=UTF-8
    Set-Cookie: PREF=ID=cd7f743bb28bd67b:FF=0:TM=1297681936:LM=1297681936:S=uohjEzE2DoJtkq92; expires=Wed, 13-Feb-2013 11:12:16 GMT; path=/;
    Set-Cookie: NID=44=s4FRgfes_aI9GwDbRkLriVRsc8jQC74yL5YUB_p6wwNGZvdAFQUL90lENKQZ5iLcgoNkUwF7Ew_s234gYLWR9gGTvw7SrZioGrTvIJ5ldHrcWYf0snw3Hb4S-mhP6yFT; expires=Tue, 16-Aug-2011 11:12:16 GMT; path=/;; HttpOnly
    Date: Mon, 14 Feb 2011 11:12:16 GMT
    Server: gws
    Content-Length: 221
    X-XSS-Protection: 1; mode=block
    <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
    <TITLE>302 Moved</TITLE></HEAD><BODY>
    <H1>302 Moved</H1>
    The document has moved
    <A HREF="">here</A>.
    $ exec 3>&-

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s