如何使用SSH2库改写Node连接Linux并获取命令返回的RESTful接口?

2026-04-06 19:231阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计195个文字,预计阅读时间需要1分钟。

如何使用SSH2库改写Node连接Linux并获取命令返回的RESTful接口?

javascriptwebssh.jsconst express=require('express');const router=express.Router();

如何使用SSH2库改写Node连接Linux并获取命令返回的RESTful接口?

/** * 自定义的ajax接口 */router.get('/', function (req, res, next) { let Client=require('ssh2').Client; let conn=new Client(); conn.on('ready', function () { // ... });});

webssh.js

const express = require('express'); const router = express.Router(); /* 自己定义的ajax接口*/ router.get('/', function (req, res, next) { let Client = require('ssh2').Client; let conn = new Client(); conn.on('ready', function () { // 下面的'ls'即为命令 conn.exec('ls', function (err, stream) { if (err) throw err; stream.on('close', function (code, signal) { conn.end(); }).on('data', function (data) { console.log('STDOUT: ' + data); res.send("连接并返回成功!!\n" + data); }).stderr.on('data', function (data) { console.log('STDERR: ' + data); }); }); }).connect({ host: '10.102.26.103', port: 22, username: 'root', password: '123456' }); }); // 注册页面,注意是在views下的相对路径 router.get('/login', function (req, res, next) { res.send("登录成功!!"); }); router.get('/:username/:password', function (req, res, next) { res.send(req.params.username + "登录成功!!"); }); // 自定义注册页面 module.exports = router;

本文共计195个文字,预计阅读时间需要1分钟。

如何使用SSH2库改写Node连接Linux并获取命令返回的RESTful接口?

javascriptwebssh.jsconst express=require('express');const router=express.Router();

如何使用SSH2库改写Node连接Linux并获取命令返回的RESTful接口?

/** * 自定义的ajax接口 */router.get('/', function (req, res, next) { let Client=require('ssh2').Client; let conn=new Client(); conn.on('ready', function () { // ... });});

webssh.js

const express = require('express'); const router = express.Router(); /* 自己定义的ajax接口*/ router.get('/', function (req, res, next) { let Client = require('ssh2').Client; let conn = new Client(); conn.on('ready', function () { // 下面的'ls'即为命令 conn.exec('ls', function (err, stream) { if (err) throw err; stream.on('close', function (code, signal) { conn.end(); }).on('data', function (data) { console.log('STDOUT: ' + data); res.send("连接并返回成功!!\n" + data); }).stderr.on('data', function (data) { console.log('STDERR: ' + data); }); }); }).connect({ host: '10.102.26.103', port: 22, username: 'root', password: '123456' }); }); // 注册页面,注意是在views下的相对路径 router.get('/login', function (req, res, next) { res.send("登录成功!!"); }); router.get('/:username/:password', function (req, res, next) { res.send(req.params.username + "登录成功!!"); }); // 自定义注册页面 module.exports = router;