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.
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:
<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>
I found the ssh2 npm to be more along the lines of what I need and have available to me to complete the task. And you'll see the simplicity also which I like. Now to have the entire process to be handled by a front end button and pipe the stdout to mongodb.
npm install ssh2
Background: Had to use escape characters for bash command Users private key path, host and username User with sudo rights Users public key in destination vm
Code:
Have you looked at the ssh2shell package?
This is probably redundent now but I thought I would add it in case someone else found it helpful.
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.
Example:
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. .
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 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 wraps SSH2.shell.
The minimum required to get SSH2shell working is:
host.onEnd
orSSH2shell.on('end')
event handler or a callback function to process the full session text closing the connection.That's it.
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 wherereq.body.value
would be the text box content.For more details about SSH2shell for SSH shell command processing see the SSH2shell readme