creeperxtsqnode.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. }
  14. async sshcon() {
  15. const config = {
  16. host: '192.168.10.102',
  17. port: 42973,
  18. userName: 'free_ly',
  19. password: '1qaz2wsx',
  20. };
  21. const pathDir = this.app.config.dataDir;
  22. if (!fs.existsSync(pathDir)) {
  23. fs.mkdirSync(pathDir);
  24. }
  25. const host = {
  26. server: config,
  27. commands: [ 'cd /home', 'qnodes' ],
  28. onCommandComplete: (command, response, sshObj) => {
  29. // console.group('complete');
  30. // console.log(`command=>${command}`);
  31. // console.log(`response=>${response}`);
  32. // console.log(`sshObj=>${sshObj}`);
  33. // console.groupEnd();
  34. },
  35. onEnd: (sessionText, sshObj) => {
  36. // console.log(`sessionText=>${sessionText}`);
  37. // console.log(`sshObj=>${JSON.stringify(sshObj)}`);
  38. const path_ = `${pathDir}/qnodes.txt`;
  39. const writeRes = fs.writeFileSync(path_, sessionText);
  40. this.ctx.service.creeperxtsread.qnodes();
  41. // fs.access(path_, err => {
  42. // if (err) {
  43. // fs.writeFile(path_, sessionText, function(err) {
  44. // if (err) throw err;
  45. // });
  46. // } else {
  47. // fs.appendFile(path_, sessionText, function(err) {
  48. // if (err) throw err;
  49. // });
  50. // }
  51. // });
  52. },
  53. };
  54. const SSH = new SSH2Shell(host);
  55. SSH.connect();
  56. }
  57. }
  58. module.exports = CreeperxtsService;