0

I am trying to run command with ssh and I am getting value not sure how to use await and async here so that I can close thread once it saves data into DB

require('dotenv').config()

const db = require('../lib/db');
const exec = require('ssh-exec')

var getModems = new Promise(function(resolve, reject) {
  var v_host = '122.12.19.160'
  exec('proxysmart-remote.sh list_online_modems', {
    user: 'root',
    host: v_host
  }).pipe(process.stdout , function (err, data) {
    resolve(data);
  })
});


const saveModems = function () {
  getModems.then(function(value) {
    // save into db
    console.log(value);
  });
}

saveModems()
process.exit();

1 Answer 1

1

You don´t need to use promises, ssh-exec returns a stream, you can bind functions to some stream events like:

let acc = '' // This is the variable where we accumulate every chunk of the stream.
exec('something')
  .on('data', function(chunk) {
    acc = acc + chunk
  })
  .on('close', function() {
    db.save(acc) // or do whatever you need.
    db.close() // Here we close the database connection how is supposed to be done.
  })
  .pipe(process.stdout)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.