Skip to content

node.js module to aid with mocking commands sent to ssh servers

License

Notifications You must be signed in to change notification settings

mdsummers/mock-ssh-responses

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mock-ssh-responses

Utility to respond with custom output and exit codes based on a given answers file.

Example Answers File

Write an answers file in yaml answers.yml

- cmd: id nobody
  stdout: uid=99(nobody) gid=99(nobody) groups=99(nobody)
  code: 0
- cmd: ls doesnotexist
  stderr: ls: cannot access 'doesnotexist': No such file or directory
  code: 2

Generate a keypair based on this answers file

Manual

Generates key for for .ssh/authorized_keys file and host entry for .ssh/config file

./cli.js -g ~/.ssh/mocked-localhost answers.yml

Follow the instructions onscreen to add the configuration to your ssh config files.

Automate Flag

Uses blank passphrase for ssh-keygen Inserts keys into ~/.ssh/authorized_keys and host entry into ~/.ssh/config file

./cli.js -a -g ~/.ssh/mocked-localhost answers.yml

Run command line mock:

$ ssh mock-answers.yml id nobody
uid=99(nobody) gid=99(nobody) groups=99(nobody)
$ ssh -i ~/.ssh/mocked-localhost localhost id nobody
uid=99(nobody) gid=99(nobody) groups=99(nobody)
$ ssh mock-answers.yml ls doesnotexist
ls: cannot access 'doesnotexist': No such file or directory
$ echo $?
2

simple-ssh use case:

mockTest.js

const host = 'localhost';
const user = process.env.USER;
const key = process.env.HOME + '/.ssh/mocked-localhost';
const cmd = "id nobody";

sshExecute.run(host, user, key, cmd, function(code, stdout, stderr) {
  if (code) {
    return callback(500, stderr);
  } else {
  callback(200, stdout);
});

sshExecute.js

exports.run = function(host, user, key, command, callback) {
  const SSH = require('simple-ssh');
  const ssh = new SSH({
    host: host,
    user: user,
    key: key
  });

  ssh.exec(command, {
    exit: function(code, stdout, stderr) {
      callback(code, stdout, stderr);
    }
  }).start();
};

About

node.js module to aid with mocking commands sent to ssh servers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published