/* eslint-disable strict */ const _ = require('lodash'); // const fs = require('fs'); const Client = require('ssh2').Client; 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(); } async sshcon() { const nowdate = moment().locale('zh-cn').format('YYYY-MM-DD'); const conn = new Client(); const pathDir = this.app.config.dataDir; if (!fs.existsSync(pathDir)) { fs.mkdirSync(pathDir); } conn.on('ready', function() { conn.exec('top -bcn 1', function(err, stream) { if (err) throw err; stream.on('close', function(code, signal) { conn.end(); }).on('data', function(data) { const path_ = `${pathDir}/top${nowdate}.txt`; fs.access(path_, err => { if (err) { fs.writeFile(path_, data.toString(), function(err) { if (err) throw err; }); } else { fs.appendFile(path_, data.toString(), function(err) { if (err) throw err; }); } }); }).stderr.on('data', function(data) { console.log('STDERR: ' + data); }); }); }).connect({ host: '192.168.10.102', port: 42973, username: 'free_ly', password: '1qaz2wsx', // privateKey: require('fs').readFileSync('/home/admin/.ssh/id_dsa') }); } } module.exports = CreeperxtsService;