I want to build a Web-based Terminal and I want to execute a shell command.
How can I use jQuery Terminal library to send command to shell and print the reply?
So to execute the shell commands you need to have a server code. The simplest way is to use, Apache and PHP. For this, you need to create a PHP script and use one of the functions that allow executing shell commands.
shell.php
<?php
if (isset($_POST['command'])) {
echo shell_exec($_POST['command']);
}
with this server-side script in place, you can write JavaScript code using jQuery Terminal that will send the request to that PHP script and display the results.
$('body').terminal(function(command) {
return fetch('shell.php').then(res => res.text());
});
What else you can do to improve the terminal is to use a unix_formatting.js file that will display any ANSI Escape code that may be generated by the shell in the browser:
<script src="https://cdn.jsdelivr.net/npm/jquery.terminal/js/unix_formatting.js"></script>
If you want to see an example of this that is more advanced, you can take a look at:
jcubic/jsh.php that was created as a single file, PHP shell that looks like a real terminal. It uses few tricks to make it more user-friendly like using unbuffer (part of expect package on GNU/Linux) and other bash tricks to make it display ANSI escapes code for every command. By default ls don't return output in colors.
If you want something even more advanced you can look at Leash Shell project that was created to give access to the shell on shared hosting.
Alternatives are:
websocket.sh which has an example of using jQuery Terminal with Unix shell via Web Sockets written as shell script.
web-console that is PHP shell that uses jQuery Terminal.
Related
I need to run javascript code using node.js from C++ application, but I don't want to save code to file before. Is it anyway possible to forward code to node.exe directly, without temporary save it to file?
yes it is. You can use the --eval flag to run code:
node --eval "console.log('hello world');"
Well you can launch node.js and then stuff the script on stdin. How to do that exactly depends on your platform.
For details google how to fork process and get it's stdin with your platform and libraries.
The title says everything..
I would like to execute special shell commands when a specific button is pressed in HTML or Javascript. If possible, can you also give me a test code??
I was wondering if I could make them auto-start in an arch live USB / CD installer and make those HTML pages as some shell command-executor.
You can execute linux commands only at backend (php, nodejs, etc).
I have to run a JavaScript file as an executable to show a dialog with standard buttons from command prompt.
I am new to command line programming with java script.
Standard Javascript is not something that natively runs in a command line environment. It is designed for use in browsers. However, Node.JS is a framework built to give you this exact feature of running Javascript as a standalone.
It can be downloaded from http://nodejs.org and installed on most platforms.
Once you have it you can invoke your javascript file by running
$ node <your file name here>.js
You cannot run javascript from the command-line. Usually javascript is executed in a browser.
You can, however, use javascript from server-site (or command-line) using Nodejs.
Have a look here on how to achieve this: NodeJs
Or you can use the REPL (Read-Eval-Print-Loop): Repl
How can I run shell command in javascript in code behind
I have this exe file on:
C:\Program Files\test.exe
that i want to run using javascript and I want to do it in codebehind or at least call this function from code behind.
The reason want to do it on code behind is that I have parameters need to pass them in the shell command.
For security reasons you can not run commands from JavaScript.
If you are using JScript, then
var shell = WScript.CreateObject("WScript.Shell");
shell.Run("command here");
I want to create a simple Javascript program with a HTML interface. The program will run in Chrome. I will also use node-serialport and Node.js to comunicate with an Arduino. I have a HTML and JavaScript file done, but I have no clue how to run it, or how to implement Node.js or node-serialport, nor how to "start" the sever. Initially it will only be running locally, but eventually it may become a real sever. For now, how do I run all that locally?
EDIT: I'm using the sample code from http://brandontilley.com/2012/03/02/controlling-an-arduino-from-nodejs.html, with the CoffeeScript converted into JavaScript.
Lucas, glad you found the blog post useful; perhaps I should add this information to it.
Getting the sketch into your Arduino
Just fire up the Arduino application, paste in the sketch code, and hit "Upload." Should be all you need to do here.
Starting the Node.js Server
What operating system are you using this on? Finding out how to access your Arduino microcontroller via node-serialport will differ based on your OS.
In the source code, change the string value of port to be your Arduino's device (once you know it). Also, the script depends on Express and (of course) node-serialport from NPM, so run npm install express serialport in the directory where your JavaScript file is saved. Finally, run the file with node server.js (assuming server.js is the name of your file). Then you can access the server at http://localhost:8080.
You can use node.js to serve up HTML with Express. If your main Javascript file is called server.js, then run it by typing:
node server.js
at the command line.