我如何使用stream.write在ssh2会话内向ttys0会话发送密码

问题描述 投票:0回答:1

我正在尝试使用流在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的写帮助器,因此我不确定是否有帮助,而不是将数据写入原始流。

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

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

node.js stream ssh2
1个回答
0
投票

我最终为ttys0会话(虚拟机)设置了自动登录,因此我不必发送密码。我从未成功发送密码而没有问题。

© www.soinside.com 2019 - 2024. All rights reserved.