Network in the Shell

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/192.168.1.33/80) &>/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/c0t0d0s0.org/80
echo -e "GET /uploads/testtar.tar HTTP/1.1\r\nhost: www.c0t0d0s0.org\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