It's a nifty small feature in the bash shell that i start to love quite a time ago when i just wanted to check some networking stuff from the shell. It's the network pseudo device in the bash shell. Not every bash shell executable is compiled with this feature, however i tested this with Solaris 11.3. Let's assume you want to find out if a port is open on a system, you could simply do this with bash build-in commands.

# (echo >/dev/tcp/ &>/dev/null && echo "TCP port 80 is open" || echo "TCP port 80 is closed"
TCP port 80 open

You just use the device with the following sequence like a position in your filesystem /dev/tcp/hostname/portnumber. There is a similar device for UDP obviously called /dev/udp. It gets really cool when you can use it to download a file to your system with it. I created a shell script like this:

$ cat getfile
exec 3<>/dev/tcp/
echo -e "GET /uploads/testtar.tar HTTP/1.1\r\nhost:\r\nConnection: close\r\n\r\n" >&3
gsed -e '1,/^.$/d' <&3 >testtar.tar

This script should work on other systems using bash as well. Just remove the g of gsed. Now start the script.

jmoekamp@nfsserver:~$ . ./getfile
jmoekamp@nfsserver:~$ ls -l testtar.tar 
-rw-r--r--   1 jmoekamp staff       3072 May  2 18:20 testtar.tar
jmoekamp@nfsserver:~$ tar -xfv testtar.tar 
tar: blocksize = 6
x testtar, 0 bytes, 0 tape blocks
x testtar/testtar, 1024 bytes, 2 tape blocks

1 Comment


  • DavidH  
    I have to tell you, this is fabulous!

    I used to do more things like this, before someone "borrowed" one of my old HTML 1.0 books... which had all kinds of examples that I would expand upon.

    Is there a fairly easy way to do such a raw shell based "getfile" with https servers?

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.