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@ \"docker inspect --format '{{ .NetworkSettings.IPAddress }}' " + username + "\"")
         .expect(" ")
         .run(function (err, stdout, exitcode) {
             if (err) {
           console.log("inside = " + stdout);
           ip = stdout;

           callback(null, ip);

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

  • docker-compose can not reach swarm
  • Insert Docker parent host ip into container's hosts file
  • install java successfully using the dockerfile however its says java command not found
  • Determine if docker image built locally or pulled from repo?
  • Couchbase Client Failure
  • Docker: container can't find domain on private network
  • But if I manually run in the shell:

    ssh core@ "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.

  • How to Resolve Unable To Login Docker
  • Conditional fig configuration for dev needs
  • How to access several ports of a Docker container inside the same container?
  • Docker Image Requirements for a GitLab-CI Runner
  • Gitlab CI Runner, DigitalOcean and Docker Compose
  • Bluemix containers - how have a Linux
  • 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()
      host: '',
      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.