I'm trying to talk to a console session inside of ssh2 session using a stream to configure some things on a machine. I am successfully able to send most data over stream.write but when it asks for password I always get 'login incorrect'.
node index.js
Client :: ready
root@debian:~#
root@debian:~# virsh console testserialcomm_5d899d2b95ca
Connected to domain testserialcomm_5d899d2b95ca
Escape character is ^]
testserialcomm login:
testserialcomm login: root
Password:
994beabda2c5ffb68f0e7ddf455a559f5952
Login incorrect
I've tried sending all sorts of variations of \r\n \n and \x0D. It's almost like a newline is getting sent before I send the actual password. I'm also not able to use readline's write helper successfully so I'm unsure if that will help rather than writing data to the stream raw.
var Client = require('ssh2').Client;
var readline = require('readline');
let loggedIn = false;//store state of logged in
let connected = false;//to know if we are connected to the virtual machine domain
let configsWritten = false;//to know if we have written configs to virtual machine yet.
let cmd1 = false;
let firstSend = false;//to know if we have sent our intial enter key
let count = 0;
var conn = new Client();
conn.on('ready', function() {
console.log('Client :: ready')
conn.shell(function(err, stream) {
if (err) throw err;
stream.setEncoding('utf-8')//utf8 helps if a multi-byte character gets split between data chunks
var rl = readline.createInterface({input: stream})//create readline interface
stream.on('close', function() {
console.log('Stream :: close');
conn.end();
}).on('data', function(data){
console.log(data)
}).on('error', function(err, stream){
throw err;
});
if(!loggedIn && !connected && !firstSend){
stream.write('\n')//the first bit of data back from the ssh session doesnt have a newline which is required by readline
//rl.write('stty -echo\n')//prevent server from echoing things back to us
firstSend = true;
}
rl.on('line', (line) => {
//process.stdout.write(`${line}\n`);
if(!loggedIn){//begin login sequence
//console.log('begin login sequence')
// if(/login\sincorrect/i.test(line)){
// lestream.write('root\x0D')
// }
if(!connected && !cmd1){
//console.log('begin get inside vm')
stream.write('virsh console testserialcomm_5d899d2b95ca\n')//first command issued after login so we get inside the vm
cmd1 = true;//set this so we don't come back here
}
if(/Escape\scharacter\s/i.test(line)){//we are connected to the virtual machine
//console.log('send enter to get login prompt')
//\x03\n//send ctrl+c to clear the login prompt in case it has invalid attempts and
stream.write('\n\n')//press enter a couple times to get login prompt to show
connected = true;
}
if(/login:/i.test(line) && connected){
//console.log('type root');
stream.write('root\n')//send container username to login prompt
}
if(/password:/i.test(line) && connected){
//console.log('send container password to login prompt
stream.write('994beabda2c5ffb68f0e7ddf455a559f5952\n')//send virtual machine password to login prompt
}
if((/[a-z0-9]\@[a-z0-9]/i).test(line) && connected){//make sure we are at a root prompt
loggedIn = true;
}
}
});
});
}).connect({//initial connection
host: 'dev.aaaaaaaaaaaaaaaa',
port: 22,
username: 'root',
password: 'aaaaaaaaaaaaa',
tryKeyboard: true
});
I expect to be able to write the password to the stream and be able to login to that console and execute other commands.
I ended up setting up auto login for the ttys0 session (virtual machine) so I didn't have to send the password. I was never able to successfully send the password without issue.