Node.js用户界面页面能否将Shell cmd运行到远程计算机并运行脚本 [英] Can Nodejs ui page run shell cmd to remote machine and run script

查看:58
本文介绍了Node.js用户界面页面能否将Shell cmd运行到远程计算机并运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望从nodejs ui网页形式运行shell命令.表单具有一个用于"ssh {计算机名}/path/to/script.sh"命令的输入字段.该页面将是nodejs单页面应用程序.是nodejs的新功能.

Looking to run a shell command from nodejs ui webpage form. Form has one entry field for a "ssh {machinename} /path/to/script.sh" command. The page will be nodejs single page application. New to nodejs.

可以做到吗?我已经搜索过,但是刚刚找到了带有php或python示例的页面.还查看了argv和shelljs,但没有人提及我正在尝试做的事情.Machine-A具有单页应用程序,并且其公共密钥已插入到Machine-B中,该shell驻留在该Shell-B中.在cli上运行命令.我希望用户在spa页面上运行自己的命令.*仅限NIX环境.

Can this be done? I have searched but just found pages with php or python examples. Also looked at argv and shelljs but no one mentions what I am trying to do. Machine-A has single-page-application and its public key already inserted into Machine-B where the executed shell script resides. Running the command on cli works. I want users to run their own commands on the spa page. *Nix environment only.

到目前为止,我有以下...

So far I have the following...

package.json:
{ 
  "name": "TP",
  "version": "0.0.1",
  "description": "Testing Project",
  "private": true,
  "dependencies": {
    "express": "3.x",
    "hbs": "*"
  }
}

表格:

<form action="" method="post" enctype="multipart/form-data" style="margin:0; width:720px;">
  <fieldset>

    <!-- ENTER COMMAND -->
    <label for="test">Enter Test:</label>
    <input style="display: inline; width: 500px;" type="text" id="test_input" name="value" placeholder="Enter test command with correct params" />

    <!-- RUN COMMAND -->
    <input type="button" onclick="runTest()" value="Run Test!" style="float: right;" />

    <!-- CLEAR COMMAND -->
    <input type="button" name="testform" value="Clear Test" onclick="this.form.reset();" style="float: right;" />
    <br />

  </fieldset>
</form>

推荐答案

现在可能已经多余了,但是我想我可以添加它,以防其他人发现它有用.

This is probably redundent now but I thought I would add it in case someone else found it helpful.

使用nodejs express可以完成页面的接收和表单数据的接收.有很多基本的网站示例,例如简单的节点网站,您可以使用它来起床.

Serving the page and receiving the form post data can be done using nodejs express. There are lots of basic site examples like simple-website-in-node that you can use to get up and going.

上面的链接提供的服务器代码示例.app.post函数中提供的SSH2shell示例.此示例未经测试.建议遵循以上教程进行设置.

Server code example provided by the link above. SSH2shell example given in the app.post function. This example is not tested. Following the tutorial above would be recommended for setup. .

var express = require('express')
    , logger = require('morgan')
    , app = express()
    , bodyParser = require('body-parser')
    , fs = require('fs')
    , Mustache = requires('mustache')
    , formPage = fs.readFileSync('./templates/myForm.xhtml');


app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use( bodyParser.urlencoded({     // to support URL-encoded bodies
    extended: true
})); 
app.use(logger('dev'))
app.use(express.static(__dirname + '/static'))

//main page route
app.get('/', function (req, res, next) {
    try {
        var html = Mustache.parse(formPage, { title: 'My Form' })
        res.send(html)
    } catch (e) {
        next(e)
    }
})

//POST route
app.post('/', function (req, res) {

    //SSH2shell implementation
    var host = {
        server:  {     
            host:         '10.5.74.123',
            port:         22,
            userName:     'cruzcontrol', 
            privateKey: fs.readFileSync('/home/cruzcontrol/.ssh/id_rsa') },
        //Here you form command is set as the only command
        commands:         [ req.body.value ]
    };

    var SSH2Shell = require ('ssh2shell')
        , SSH = new SSH2Shell(host)
        , callback = function( sessionText ){
              //code here to return the sessionText and command in the form page.
              var html = Mustache.parse(formPage, { title: 'My Form',
                  value: req.body.value, 
                  result: sessionText});
              res.send(html);
        }       

    SSH.connect(callback);

})

app.listen(process.env.PORT || 3000, function () {
  console.log('Listening on http://localhost:' + (process.env.PORT || 3000))
})

只要node express服务于您的页面,SSH2shell就可以从表单提交中接收您的命令(值),运行它并在返回的表单页面中显示结果和命令.该示例使用模板引擎Mustache,因此myForm.xhtml需要页面表单html和几个标签来输出命令和响应.

As long as node express was serving your page SSH2shell could receive your command (value) from the form submission, run it and display the result and the command in a returned form page. The example uses the templating engine Mustache so the myForm.xhtml would need the page form html and a couple of tags to output the command and response.

SSH2shell没有用于检查是否设置了密码或任何其他连接参数的代码,因此,如果您不需要它来进行身份验证,则不必使用它.

SSH2shell has no code that checks that a password is set or any of the other connection parameters so if you don't need it to authenticate then you don't have to use it.

SSh2shell包装SSH2.shell.

SSh2shell wraps SSH2.shell.

要使SSH2shell正常运行,最低要求是:

The minimum required to get SSH2shell working is:

  • host.server选项.
  • 要运行的命令的host.commands数组.
  • 使用 host.onEnd SSH2shell.on('end')事件处理程序或回调函数来处理关闭连接的完整会话文本.
  • The host.server options.
  • The host.commands array of commands to run.
  • Either use the host.onEnd orSSH2shell.on('end') event handler or a callback function to process the full session text closing the connection.

就是这样.

可以通过使用文本框来处理多个命令,或者使用定界符来分割命令,或者将每一行都视为命令,然后将其压入命令数组.可以使用命令数组变量设置 host.commands 属性.响应(sessionText)处理基本上相同,除了 req.body.value 是文本框内容.

Multiple commands could be handled by using a text box and either use a delimiter to split the commands or treating each line as a command then pushing them onto a command array. host.commands property can be set with the commands array variable. Response (sessionText) handling would be basically the same except where req.body.value would be the text box content.

有关用于SSH shell命令处理的SSH2shell的更多详细信息,请参见 SSH2shell自述文件

For more details about SSH2shell for SSH shell command processing see the SSH2shell readme

这篇关于Node.js用户界面页面能否将Shell cmd运行到远程计算机并运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆