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 192.168.1.1 on port 15123 using:

echo "Hello world" > /dev/tcp/192.168.1.1/15123

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/www.google.com/80
    $ echo "GET /" >&3
    $ cat <&3
    HTTP/1.0 302 Found
    Location: http://www.google.co.uk/
    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=/; domain=.google.com
    Set-Cookie: NID=44=s4FRgfes_aI9GwDbRkLriVRsc8jQC74yL5YUB_p6wwNGZvdAFQUL90lENKQZ5iLcgoNkUwF7Ew_s234gYLWR9gGTvw7SrZioGrTvIJ5ldHrcWYf0snw3Hb4S-mhP6yFT; expires=Tue, 16-Aug-2011 11:12:16 GMT; path=/; domain=.google.com; 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="http://www.google.co.uk/">here</A>.
    </BODY></HTML>
    $ exec 3>&-
    $

Leave a comment