I have the following code:
var sshPromise=require('ssh2-promise');
var x={
host:'host',
username:'username',
password:'mypassword'
};
var ssh=new sshPromise(x);
ssh.connect().then(()=>{
console.log('connected...');
});
//Get a shell session
ssh.shell().then((socket) => {
socket.on('data', (data) => {
console.log(data.toString());
})
//Can write to socket
//console.log('here<--');
socket.write('my password');// <----sending password to enter the server
});
what is trying to do here is I'm creating a shell session after connecting to a server.After connecting it asks me password to access the server and i want to send my password but it does nothing.
I did a little debugging by adding console.log('here<--') and realised that it gets printed right at the start before the data arrives, meaning the socket.write('my password') gets executed before the data arrives.
I can't figure out how to solve this. Can anyone please help me.
EDIT:
const socket = await ssh.shell();
const data = await new Promise(resolve => socket.on('data', resolve));
socket.write('my password\n');
//to fetch next shell output
const data_1= await new Promise(resolve => socket.on('data', resolve));
is there any way to call use the data multiple times rather than creating a new promise everytime i need to get output from the shell?