0

app.js:

const fs = require('fs')
const path = require('path')
const {NodeSSH} = require('node-ssh')
const ssh = new NodeSSH()
ssh.connect({
    host: '192.168.0.2',
    username: 'ubuntu',
    password: '123456'
})
.then(function() {
    ssh.execCommand('virsh list --all', {}).then(function(result) {
        console.log(result);
        return;
    })
})

Return:

Id  Name State
--------------------

But the command virsh list --all works and return complete list when using ssh connect via command line. I also test another command in app.js like ls or pwd , and works normally.

Thanks for helping.

徐奕群
  • 3
  • 1
  • Are you sure the guest domains are running under the user "ubuntu"? Is it possible you're either logged in as root or prefixing the command with "sudo" when you are trying the command via your interactive SSH client? – mscdex Apr 19 '22 at 01:15
  • Additionally it's possible that your interactive shell session startup script sets some environment variables that `virsh` is using. Non-interactive command execution via ssh does not execute these shell session startup scripts, so that could be another difference. – mscdex Apr 19 '22 at 01:17

1 Answers1

0

Replace your command with:

bash -l -c "virsh list --all"
Henry Wu
  • 16
  • 3
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Apr 19 '22 at 17:24