Database JSON Proxy on Bash in 5 minutes
or another way to give JSON from MySQL
This post was created as a result of purely academic interest. It all started when I was developing a small Javascript client application for my own needs and I needed to interface with an existing database where the data I was looking for were stored. The base is MySQL. One of the easiest way is to implement a server-side script (in PHP, Python, Nodejs or any other programming language), which makes the required query using the incoming parameters and returns the result in JSON form.
It’s no secret that Netcat can be turned into a WEB server by adding a couple of lines to the bash. By taking this idea and refining it, we can get a simple bash analogue of DBSlayer in five minutes.
First, we will write a simple helper utility to convert the output from the mysql console client to JSON format:
It’s easy to use:
Now the server code itself, which we put in the dbjs.sh file:
That’s all there is to it. Run it and see something like this in the console:
Then we just address to our server, on the specified port and get the output:
The script is not perfect, but no one prevents him to refine it, if at all there is a need.