creeperxtsqnode.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* eslint-disable strict */
  2. const _ = require('lodash');
  3. // const fs = require('fs');
  4. const SSH2Shell = require('ssh2shell');
  5. const fs = require('fs');
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. class CreeperxtsService extends CrudService {
  8. async creeper() {
  9. // 链接linux服务器
  10. console.log('进入连接linux服务器');
  11. // await this.sshcon();
  12. await this.sshcon();
  13. console.log('集群节点结束');
  14. }
  15. async sshcon() {
  16. const redis = this.app.redis;
  17. const config = {
  18. host: '192.168.10.102',
  19. port: 42973,
  20. userName: 'free_ly',
  21. password: '1qaz2wsx',
  22. };
  23. const pathDir = this.app.config.dataDir;
  24. if (!fs.existsSync(pathDir)) {
  25. fs.mkdirSync(pathDir);
  26. }
  27. const host = {
  28. server: config,
  29. commands: [ 'cd /home', 'qnodes' ],
  30. onCommandComplete: (command, response, sshObj) => {
  31. // console.group('complete');
  32. // console.log(`command=>${command}`);
  33. // console.log(`response=>${response}`);
  34. // console.log(`sshObj=>${sshObj}`);
  35. // console.groupEnd();
  36. console.log('command ok');
  37. },
  38. onEnd: async (sessionText, sshObj) => {
  39. // console.log(`sessionText=>${sessionText}`);
  40. // console.log(`sshObj=>${JSON.stringify(sshObj)}`);
  41. const text = JSON.stringify(sshObj.sessionText);
  42. const res = await redis.set('qnodes', text);
  43. if (res === 'OK') await this.ctx.service.creeperxtsread.qnodes();
  44. },
  45. };
  46. try {
  47. const SSH = new SSH2Shell(host);
  48. SSH.connect();
  49. } catch (error) {
  50. console.log(error);
  51. console.log('集群节点获取失败');
  52. }
  53. }
  54. }
  55. module.exports = CreeperxtsService;