Part01 — TCP/UDP request with a native Bash feature and without curl/wget/*

Sending HTTP / TCP / UDP requests via Bash without using any other tool

Matheus Lozano
Bash Tips and Tricks
2 min readJul 5, 2019

--

bash: wget: command not found
bash: curl: command not found

Explaining how it works

How many times you tried to test an application or service and you couldn't because you don't have curl/wget/telnet/* installed and the user doesn't have permission to install? Well, the good news is: You can do it with Bash, by native.

One of the optional features (also a default one) for Bash is the --{enable,disable}-net-redirections. With this feature, you can send TCP or UDP requests in many ways.

If host is a valid hostname or Internet address, and port is an integer port number or service name, Bash attempts to open the corresponding TCP/UDP socket.

It's possible to do it because of the pseudo devices under the /dev/. They're /dev/tcp and /dev/udp.

The structure for this feature/pseudo devices are:
/dev/PROTOCOL/HOST/PORT

For example:
/dev/tcp/google.com/443
/dev/udp/192.168.0.10/123

So, base on that, you can send many times of requests using this Bash feature, from HTTP to NTP. Also, you can get the result back, if you want.

By default, the feature net-redirections is enabled on Bash.

A simple example

To send this request, I create the examples below. These examples are sending a true value to localhost and also google.com.

A possible error

No such file or directory => If you're getting this error during the execution, it's because your Bash don't have the feature net-redirections enabled.

For example:

:> /dev/tcp/google.com/443
bash: /dev/tcp/google.com/443: No such file or directory

: Is the short version of true. Could also be:
true > /dev/tcp/google.com/443

To fix this error, you can use this script. It will install Bash with net-redirections. This script can be used on Ubuntu, CentOS and Alpine.

--

--

Bash Tips and Tricks
Bash Tips and Tricks

Published in Bash Tips and Tricks

Bash tips and tricks that will save some time and lines. Writing a bash script it's easy, but are you using all Bash's power? Let's see !

Matheus Lozano
Matheus Lozano

Written by Matheus Lozano

A SysAdmin who love to automate everything — DevSecOps, SRE and Chaos Engineer, let's share our skills.