
I build IoT applications on a daily basis at RAZRLAB, and every once in awhile, I do need the internal tools to perform some functions on servers, be it executing commands or forwarding ports. For this, I found a neat little tool node-ssh which helps establish ssh tunnels to servers with key authentication.
Let's create a new folder and set up a fresh npm repository
npm init --y
We are going to use the following dependencies:
We will create our SSH tunnel using private key authentication for that we will create SSH key pair with a passphrase.
I would recommend using your cloud providers SSH key access management to securely manage access to your servers. Click here to know how to manage SSH key access through Digital Ocean.
Now, let's specify paths and information in the .env file
# .env
SSH_host=
SSH_user=
SSH_privatekey=
SSH_passphrase=
Environment variable definitions:
SSH_host : Host IP Address or DNS name of the serverSSH_user : Host username for logging inSSH_privatekey : path to the private keySSH_passphrase : passphrase for the key for added layer of securityWe will access the server and install a Redis server. We will use an Ubuntu machine and leverage the apt package manager to install the Redis server.
ssh <username>@<host>
sudo apt-get install redis-server
This will download and install Redis and its dependencies. It will also export a CLI application called redis-cli to interact with the Redis server.
Now, that we have done the setup. Let's write a minimal client that will navigate to a directory and access the redis-cli to ping Redis server
const node_ssh = require('node-ssh');
const ssh = new node_ssh();
require('dotenv').config();
ssh
.connect({
host: process.env.SSH_host,
username: process.env.SSH_user,
privateKey: process.env.SSH_privatekey,
passphrase: process.env.SSH_passphrase,
})
.then(() => {
ssh.execCommand('redis-cli ping', { cwd: '/var/www' }).then((result) => {
console.log('STDOUT: ' + result.stdout);
console.log('STDERR: ' + result.stderr);
});
});
As you can see in the example above, we read the output from STDOUT and STDERR for output and errors respectively, after running the ping command through redis-cli.
The neat thing about node-ssh, is that it is a simple Promise wrapper and hence you can use async/await logic with it.
STDOUT: PONG;
Now, that you have been able to SSH to a remote server and have been able to run CLI commands, the next step would be to go crazy π. You now have endless possibilities of automating deployments and executing commands on your servers leveraging the power of building complex applications using Node.js.
Happy Grizzly Coding π» !


Node.js 24 is here with game-changing features like V8 13.6, Float16Array, explicit resource management, WebAssembly Memory64, and npm 11. Learn what's new and how to upgrade smoothly.
This blog post focuses on the new experimental feature in React 18 called the use hook. It explains how the use hook can be used to create custom hooks that can be reused across different components, simplifying state management and making code more modular.
In this blog post we'll explore how Next.js can help you optimize your website for search engines From server-side rendering to automatic code splitting we will cover all the features that make Next.js a powerful tool for SEO optimization