1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- /* eslint-disable strict */
- const _ = require('lodash');
- // const fs = require('fs');
- const SSH2Shell = require('ssh2shell');
- const fs = require('fs');
- const moment = require('moment');
- 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: [ 'top -bcn 1' ],
- onCommandComplete: (command, response, sshObj) => {
- 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('top', text);
- if (res === 'OK') await this.ctx.service.creeperxtsread.top();
- await this.ctx.service.creeperxtsqnode.creeper();
- },
- };
- try {
- const SSH = new SSH2Shell(host);
- SSH.connect();
- } catch (error) {
- console.log(error);
- console.log('用户监控失败');
- }
- }
- }
- module.exports = CreeperxtsService;
|