Connessione SSH in Node.js con node-ssh

Mattepuffo's logo
Connessione SSH in Node.js con node-ssh

Connessione SSH in Node.js con node-ssh

node-ssh è una libreria per Node.js per connetterci ad un server SSH.

In questo articolo vediamo come usarla per connetterci ed eseguire un comando.

Per l'installazione possiamo usare npm:

npm install node-ssh

Qui sotto un esempio di codice:

const {NodeSSH} = require('node-ssh');

const ssh = new NodeSSH()

ssh.connect({
  host: 'HOST',
  username: 'USER',
  password: 'PASSWORD',
}).then(() => {
  ssh.execCommand('ls -la')
      .then((result) => {
        console.log('STDOUT: ' + result.stdout)
        console.log('STDERR: ' + result.stderr)
      });
}).catch((err) => {
  console.error('ERRORE: ' + err);
});

Enjoy!


Condividi

Commentami!