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”

  • How to run docker to other server machine
  • docker: possible to create a single file with binaries and packages installed?
  • docker-machine fails to start host
  • Running a Docker image in PyCharm causes “Invalid volume specification”
  • Deploy Cloudsuite benchmark using Docker swarm mode
  • yarn install error Couldn't find a package.json during Docker build
  • 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.

  • Specify the env file docker compose uses
  • sh on docker: syntax error: unexpected end of file (expecting “then”)
  • Docker exposed ports are not available
  • Kubernetes - dynamic provisioning of storage
  • Docker ERROR: for app Container command 'docker-php-entrypoint' not found or does not exist
  • Docker proxy windows daemon named pipe to Ubuntu Container
  • 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.