/* eslint-disable strict */ const _ = require('lodash'); // const fs = require('fs'); const SSH2Shell = require('ssh2shell'); const fs = require('fs'); const { CrudService } = require('naf-framework-mongoose/lib/service'); class CreeperxtsService extends CrudService { async creeper() { // 链接linux服务器 console.log('进入连接linux服务器'); // await this.sshcon(); await this.sshcon(); console.log('集群节点结束'); } async sshcon() { const redis = this.app.redis; const config = { host: '192.168.10.102', port: 42973, userName: 'free_ly', password: '1qaz2wsx', }; const pathDir = this.app.config.dataDir; if (!fs.existsSync(pathDir)) { fs.mkdirSync(pathDir); } const host = { server: config, commands: [ 'cd /home', 'qnodes' ], onCommandComplete: (command, response, sshObj) => { // console.group('complete'); // console.log(`command=>${command}`); // console.log(`response=>${response}`); // console.log(`sshObj=>${sshObj}`); // console.groupEnd(); console.log('command ok'); }, onEnd: async (sessionText, sshObj) => { // console.log(`sessionText=>${sessionText}`); // console.log(`sshObj=>${JSON.stringify(sshObj)}`); const text = JSON.stringify(sshObj.sessionText); const res = await redis.set('qnodes', text); if (res === 'OK') await this.ctx.service.creeperxtsread.qnodes(); }, }; try { const SSH = new SSH2Shell(host); SSH.connect(); } catch (error) { console.log(error); console.log('集群节点获取失败'); } } } module.exports = CreeperxtsService;