背景:
公司要实现防火墙联动功能,防火墙只能通过命令行控制,需要开发一个接口,实现一键封堵功能。
解决思路
接口使用nodejs开发,使用nodejs实现ssh功能,管理防火墙。
实现过程
npm组件node-ssh支持使用ssh命令,在linux测试通过
使用方法如下:
`const node_ssh = require('node-ssh');
const ssh = new node_ssh();`
return new Promise(( resolve, reject ) => {
ssh.connect({
host: ip,
username: "username",
password: "password",
port:22 //SSH连接默认在22端口
}).then(function () {
ssh.execCommand(command, {}).then(function (result) {
if (!result.stderr) {
resolve( result.stdout )
}else{
reject( result.stderr )
}
});
}).catch(err=>{
reject( err )
console.log('s