creeperxts.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 moment = require('moment');
  7. const { CrudService } = require('naf-framework-mongoose/lib/service');
  8. class CreeperxtsService extends CrudService {
  9. async creeper() {
  10. // 链接linux服务器
  11. console.log('进入连接linux服务器');
  12. // await this.sshcon();
  13. await this.sshcon();
  14. console.log('用户监控结束');
  15. }
  16. async sshcon() {
  17. const redis = this.app.redis;
  18. const config = {
  19. host: '192.168.10.102',
  20. port: 42973,
  21. userName: 'free_ly',
  22. password: '1qaz2wsx',
  23. };
  24. const pathDir = this.app.config.dataDir;
  25. if (!fs.existsSync(pathDir)) {
  26. fs.mkdirSync(pathDir);
  27. }
  28. const host = {
  29. server: config,
  30. commands: [ 'top -bcn 1' ],
  31. onCommandComplete: (command, response, sshObj) => {
  32. console.log('command ok');
  33. },
  34. onEnd: async (sessionText, sshObj) => {
  35. // console.log(`sessionText=>${sessionText}`);
  36. // console.log(`sshObj=>${JSON.stringify(sshObj)}`);
  37. const text = JSON.stringify(sshObj.sessionText);
  38. const res = await redis.set('top', text);
  39. if (res === 'OK') await this.ctx.service.creeperxtsread.top();
  40. await this.ctx.service.creeperxtsqnode.creeper();
  41. },
  42. };
  43. try {
  44. const SSH = new SSH2Shell(host);
  45. SSH.connect();
  46. } catch (error) {
  47. console.log(error);
  48. console.log('用户监控失败');
  49. }
  50. }
  51. }
  52. module.exports = CreeperxtsService;