如何使用 stream.write 将密码发送到 ssh2 会话中的 ttys0 会话

How can I send password to ttys0 session inside ssh2 session using stream.write

我正在尝试与 ssh2 会话中的控制台会话对话,使用流在机器上配置一些东西。我能够成功地通过 stream.write 发送大部分数据,但是当它要求输入密码时,我总是得到 '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

我试过发送 \r\n \n 和 \x0D 的各种变体。这几乎就像在我发送实际密码之前发送了一个换行符。我也无法成功使用 readline 的写入助手,所以我不确定这是否有帮助而不是将数据写入原始流。

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
});

我希望能够将密码写入流并能够登录到该控制台并执行其他命令。

我最终为 ttys0 会话(虚拟机)设置了自动登录,所以我不必发送密码。我一直无法顺利发送密码。