sshlistconfig: a simple cli tool in Node.js

March 23, 2017

Most web developers use SSH extensively. With the ability to use public keys to automate server login and config files to keep accounts organized, it’s a core component of how we do our work. Additionally, the department I work in has standardized around using unprivileged “deploy” accounts exclusively to handle code deployments to client servers. As a result I typically have at least two and as many as six separate SSH accounts to keep track of per client I work with.

Net result: if I haven’t worked with a particular client’s servers in a couple months I have to go look at my SSH config file to remember what the host name is for the particular SSH account I want to access.

Running cat ~/.ssh/config returns a bunch of extraneous info I don’t need, so I decided it would be fun to write a small script that just listed the host names in my SSH config file. And thus, sshlistconfig was born.

Here’s a quick specification:

Goals:

  • Get zsh to recognize my file as a shell script and execute
  • Open my SSH config file
  • Parse contents
  • Display host lines only, omitting the “host “ prefix

And code:

Goal 1 is no sweat. Anyone who has prior experience scripting in a *nix environment should be familiar with starting a text file with #! (shebang) followed by the path to the interpreter that should parse the contents of the script. Node is no different in this regard than perl or any other commonly used scripting language. Example:

#!/usr/local/bin/node 

For goal 2 we’re going to rely on Node’s File System library to handle file I/O. To do this we need to first include it as a required module in our script:

var fs = require(‘fs’);

3 & 4 are merely bog-standard file parsing and printing output:

fs.readFile('/Users/freeman/.ssh/config', 'utf8', function(error, data) {
  if (error) throw error;
  var lines = data.split(/\r?\n/);
  for (var i = 0; i < lines.length; ++i) {
    if (lines[i].includes('host ')) {
      var host = lines[i].replace('host ', '');
      console.log(host);
    }
  }
});

Full code:

#!/usr/local/bin/node
var fs = require('fs');
fs.readFile('/Users/freeman/.ssh/config', 'utf8', function(error, data) {
  if (error) throw error;
  var lines = data.split(/\r?\n/);
  for (var i = 0; i < lines.length; ++i) {
    if (lines[i].includes('host ')) {
      var host = lines[i].replace('host ', '');
      console.log(host);
    }
  }
});

Making the command available:

With the script written the last thing to do is link to it in a folder that is part of PATH. This typically involves adding the folder the script resides in to PATH via ~/.zshrc or similar. I chose to place a symlink to the script in /usr/local/bin.

Now I can easily display a list of configured SSH hosts without having to peek at the contents of ~/.ssh/config

➜  ~ sshlistconfig
client1-deploy
client1-root
client2-deploy
client2-root
client2-db
client3-dev
client4-dev-deploy
client4-staging-deploy
client4-prod-deploy
client4-db-root

Additional reading:


Add new comment