Rapid7 Blog

Unleash the power of node.js for Shell Scripting (Part 1)

POST STATS:

SHARE

Setting Up

Running a Node Script

We are going to talk about creating shell scripts with node.js. The first thing that you need to do is install node.js. You can get the installers from https://nodejs.org/en/download/, or use your favorite package manager.

Let’s go straight in and write our first script.

The first thing to do is to create a file called script.js with the following code:

console.log('hello world')

We can now execute this script by running the following command in our shell:

node script.js

If you are on a Unix­-like system, you might prefer to directly create an executable file in 2 steps:

Tell the operating system what the interpreter for the code with Shebang. You can do that putting this “special” line at the top of your script which means that your script will now look like this:

#!/usr/bin/env node console.log('hello world')

Give that file execution permissions:

chmod +x script.js

Now, you can execute that script directly by using:

./script.js

And, that’s it… Ok, ok, let me show you a few more interesting things with node!

Reading from files

Usually when you read from a file, you don’t want to block your execution thread. This means that you don’t need to load all the file in memory at the same time, especially for long files. The following is a simple example on how to do it:

But there may be exceptions when you need to read the whole file! Maybe you need to load a configuration file before anything else happens or any other good reason you can think of. In this case, you can load files as follow:

Pipes

An interesting option when you are working in shell is the possibility to use pipes to chain the output of one command to the input of the next one. For example, the following will send the output of ps to the input of grep and keep only the lines containing “python”:

“ps | grep python”

In node.js, you can deal with the standard input of your script using process.stdin. The following example will print the input to the console

Now, using that script, we can print “hello world.” by using the following line:

echo 'hello world.' | ./index.js

Next, the natural step here is to start thinking about sending our output to another process using pipe. As we do so, we quickly realise that there is a risk of getting disgusting errors in our output (eg. after runtime issues). I won’t go into details but if you want to look a bit more into this, I’d recommend to have a look at EPIPE BOMB and this project for a possible solution.

Note: to see the type of error I described in the above paragraph, you can execute something like:

ls | ./index.js | this_command_doesnt_exist

Managing Dependencies

I’m going to assume you’re already familiar with npm. If not, this page is your friend.

First we need to create the package.json file. With this file, we will be able to use and share our scripts without adding all the Javascript code from the 3rd party modules we use. By sharing our code and that file, other users will be able to install all dependencies using npm install. For example, let’s create a package.json file that looks like this:

Now you can see a new folder called node_modules and in it the cheerio and request code. Additionally, the versions of these will be equal or higher than the ones we had put in our package.json file without going up a major version (thanks to the ^ characters).

Environment Variables and Script Arguments

To access environment variables in your script, you just need to use the process.env property. For example, to get $HOME, you can use process.env.HOME (if you are thinking about using this to pass configuration variables to your script right now, I would think you should also have a look at the nconf project). You can use process.argv to use arguments passed into your script. It is an array containing the full list of arguments.

If you need to manage your arguments nicely, you should use the Commander module. Here’s an interesting way to manage you package.json file when you identify the need for a new dependency (here, Commander). You just need to run the following command (in your project directory):

npm install commander ­­save

This will have done two things:

downloaded the Commander project

updated you package.json file to include that dependency (with the correct version

Now, here’s an example on how to use Commander (call your file commander_example.js):

You should start testing this code by running ./commander_example.js ­­help. So much fun! For more details, have a look at the npm Commander’s page. In a future blogpost, we will write an interesting script using most of the elements we described today. Keep an eye on this blog and you will be able to see how you can quickly put all those interesting concepts into practice.