/dev/tcp to do fast and light TCP health checks

arberx1 pts0 comments

More on Using Bash's Built-in /dev/tcp File (TCP/IP) | Linux Journal

Skip to main content

HOW-TOs

More on Using Bash's Built-in /dev/tcp File (TCP/IP)

by Mitch Frazier

on September 30, 2009

If you saw yesterday's Tech Tip and were looking for more<br>on using TCP/IP with bash's built-in /dev/tcp device file then read on.<br>Here, we'll both read from, and write to a socket.

Before I go any further, let me state that this is based on<br>something I discovered here on Dave Smith's Blog. All I've done here<br>is added a few improvements based on the comments to the original post.<br>I've also added a bit of additional explanation.

The following script fetches the front page from Google:

exec 3<>/dev/tcp/www.google.com/80<br>echo -e "GET / HTTP/1.1\r\nhost: http://www.google.com\r\nConnection: close\r\n\r\n" >&3<br>cat

Pretty simple, just 3 lines. The first line may be a bit confusing<br>if you haven't seen this type of thing before.<br>This line causes file descriptor 3 to be opened for reading and writing<br>on the specified TCP/IP socket. This is a special form of the<br>exec statement. From the bash man page:

exec [-cl] [-a name] [command [arguments]]

... If command is not specified, any redirections<br>take effect in the current shell, and the return status is 0.

So using exec without a command is a way to open files in the current shell.

After the socket is open we send our HTTP request out the socket with<br>the echo ... >&3 command. The request consists of:

GET / HTTP/1.1<br>host: http://www.google.com<br>Connection: close

Each line is followed by a carriage-return and newline, and all<br>the headers are followed by a blank line to signal the end of<br>the request (this is all standard HTTP stuff).

Next we read the response out of the socket using cat , which reads<br>the response and prints it out. The response being the main HTML page<br>from Google:

$ bash tcp.sh<br>HTTP/1.1 200 OK<br>Date: Wed, 30 Sep 2009 17:28:36 GMT<br>Expires: -1<br>Cache-Control: private, max-age=0<br>Content-Type: text/html; charset=ISO-8859-1<br>Set-Cookie: PREF=ID=...<br>Set-Cookie: NID=27=...<br>Server: gws<br>X-XSS-Protection: 0<br>Transfer-Encoding: chunked<br>Connection: close

fef

And that's it, with just a few more lines of code you could have your<br>own bash based browser... well maybe not.

Mitch Frazier is an embedded systems programmer at Emerson Electric Co. Mitch has been a contributor to and a friend of Linux Journal since the early 2000s.

Load Disqus comments

Our discussions are powered by Disqus, which require JavaScript.

Recent Articles

NanoKVM-Go Brings AI-Powered Hardware Control to Linux with a Compact USB-C KVM<br>George Whittaker

AI Uncovers a 15-Year-Old Linux Kernel Root Vulnerability Hidden Since 2011<br>George Whittaker

Azure Linux 4.0 Released: Microsoft Expands Its Enterprise Linux Platform Beyond the Cloud<br>George Whittaker

KDE Plasma 6.7.1 Released with Stability Fixes, UI Improvements, and Better Wayland Reliability<br>George Whittaker

PorteuX 2.6 Released with Linux 6.19, TLP Support, and Smarter Hardware Optimization<br>George Whittaker

CachyOS June 2026 ISO Released with Hyprland Noctalia, Faster Performance, and Smarter System Tools<br>George Whittaker

&times;

linux http bash george whittaker using

Related Articles