0

我正在尝试使用流与 ssh2 会话中的控制台会话交谈,以在机器上配置一些东西。我能够成功地通过 stream.write 发送大部分数据,但是当它要求输入密码时,我总是得到“登录不正确”。

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 的 write helper,所以我不确定这是否会有所帮助,而不是将数据写入原始流。

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

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

4

1 回答 1

0

我最终为 ttys0 会话(虚拟机)设置了自动登录,所以我不必发送密码。我永远无法成功发送密码而没有问题。

于 2020-03-13T13:56:03.893 回答