12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- /* eslint-disable strict */
- const _ = require('lodash');
- // const fs = require('fs');
- const SSH2Shell = require('ssh2shell');
- const fs = require('fs');
- 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 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: [ 'cd /home', 'qnodes' ],
- onCommandComplete: (command, response, sshObj) => {
- // console.group('complete');
- // console.log(`command=>${command}`);
- // console.log(`response=>${response}`);
- // console.log(`sshObj=>${sshObj}`);
- // console.groupEnd();
- },
- onEnd: (sessionText, sshObj) => {
- // console.log(`sessionText=>${sessionText}`);
- // console.log(`sshObj=>${JSON.stringify(sshObj)}`);
- const path_ = `${pathDir}/qnodes.txt`;
- const writeRes = fs.writeFileSync(path_, sessionText);
- this.ctx.service.creeperxtsread.qnodes();
- // fs.access(path_, err => {
- // if (err) {
- // fs.writeFile(path_, sessionText, function(err) {
- // if (err) throw err;
- // });
- // } else {
- // fs.appendFile(path_, sessionText, function(err) {
- // if (err) throw err;
- // });
- // }
- // });
- },
- };
- const SSH = new SSH2Shell(host);
- SSH.connect();
- }
- }
- module.exports = CreeperxtsService;
|