Why this SSH command on Nexpect does not work?

I am trying to run and get information about docker containers from inside a docker container, to do this I am using Nexpect, but I found a strange error:

exports.getIP = function(username, callback){
   var nexpect = require('nexpect');
   nexpect.spawn("ssh core@172.17.8.101 \"docker inspect --format '{{ .NetworkSettings.IPAddress }}' " + username + "\"")
         .expect(" ")
         .run(function (err, stdout, exitcode) {
             if (err) {
                 callback(err);
             }
           console.log("inside = " + stdout);
           ip = stdout;

           callback(null, ip);
    });
};

The error response for this method is “Error : Command not found: ssh”

  • Docker-compose Error
  • docker-compose exec failed: executable file not found in $PATH
  • docker-compose up -d <name>; No such service: <name>
  • Dockerizing a maven app that uses oracle-ojdbc: How do you get the driver in there?
  • Docker project structure with git submodules
  • Pacman: How to choose default choice when it asks to replace a package?
  • But if I manually run in the shell:

    ssh core@172.17.8.101 "docker inspect --format '{{ .NetworkSettings.IPAddress }}' test"
    

    Works perfectly.

    I also have very similar functions to run Docker using the Nexpect and SSH and it is working fine. After some tests I think it is something with the \ character but for this specific command I have to use quotation marks on the string.

  • Wordpress on Docker: mysqld entered FATAL state, too many start retries too quickly
  • Service host/port undefined, Kubernetes/Google Container Engine
  • Docker - Running Apache and Multiple JARs
  • Execute command on host during docker build
  • Chain automated builds in the same Docker Hub repository
  • Possible differences between 2 identical Docker containers
  • One Solution collect form web for “Why this SSH command on Nexpect does not work?”

    You could use the ssh2 module instead of spawning a child process and using nexpect:

    var Connection = require('ssh2');
    
    var ssh = new Connection();
    ssh.on('ready', function() {
      ssh.exec("docker inspect --format '{{ .NetworkSettings.IPAddress }}' test", function(err, stream) {
        if (err)
          throw err;
    
        stream.on('exit', function(code, signal) {
          console.log('>> Docker exited with code %d, signal %s', code, signal);
        }).on('close', function() {
          // this indicates that there is no more stdout/stderr output,
          // just like with node's spawn()
          ssh.end();
        }).pipe(process.stdout);
    
        stream.stderr.pipe(process.stderr);
      });
    }).connect({
      host: '172.17.8.101',
      username: 'core',
      agent: process.env.SSH_AUTH_SOCK,
      // or if you have a specific key you want to use:
      //privateKey: require('fs').readFileSync(process.env.HOME + '/.ssh/id_rsa')
    });
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.