How to execute linux commands from HTML and Javascript pages? - javascript

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).

Related

How to send a command to the shell with jQuery Terminal?

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.

Node-webkit app talk to terminal

Is it possible for me to write html/css/javascript to have a button that'll open up the terminal program on my computer and run some scripts?
The scenario I want is:
On my ubuntu laptop, run my node-webkit app, click a button on the screen, a terminal opens, and start executing some scripts.
Thanks
from web page you cant do this but you can use node.js , if you want to make a GUI app with JavaScript you can use node-webkit with it you can build cross platform software that can work with Linux and execute commands in the terminal
You can do this from a webpage, but make sure your hosts.allow (or equivalent) is set to localhost only. Just run any web server with cgi capability and drop in your cgi script that runs your scripts into the cgi-bin directory. Then just add an empty form submit button or link to localhost:8080/cgi-bin/your_script
Slitaz uses this for their entire configuration system using busybox httpd and shell scripts, but uses shell generated interactive forms instead of a terminal. See: http://hg.slitaz.org/tazpanel/file/
Another way to do it without a web server (at least with firefox/seamonkey - not tried with chrom) is to associate a file extension so that files with that extension are opened with that script. Then just make a link to an empty file with that extension.
Use child_process module:
<script>
var cp = require('child_process');
function run() {
cp.exec('gnome-terminal -x bash -c "./your_script.sh; read -n1"');
}
</script>
<button onclick="run()">your_script.sh</button>
You can also remove read -n1 and simply run
cp.exec('gnome-terminal -x ./your_script.sh');
in case you don't want to wait for any key press.

how to run a js file in command prompt

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

Running a local server with javascript

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.

Script/App that automatically compiles and compresses js files as you save

I'm building a web site and have multiple js files all in one directory. When I save any one of the js files I want a script to run that will compile and compress all files using the google closure compiler jar.
Example from Google Closure Compiler README:
java -jar compiler.jar --js=in1.js --js=in2.js ... --js_output_file=out.js
Is there a shell script or app that does this? I'm looking for something similar to how http://incident57.com/less/ works for CSS.
In linux you can use the inotifywait command to listen for changes in a specific folder.
This script can you give an idea:
#!/bin/bash
directory=$1
inotifywait -q -m --format '%f' -e modify -e move -e create -e delete ${directory} | while read line
do
echo "doing something with: $line";
# for example:
# java -jar compiler.jar --js=in1.js --js=in2.js ... --js_output_file=out.js
done
You can invoke this script specifying the "monitor" directory, in this way
./inotify.sh ~/Desktop/
Linux and Mac OSX have application interfaces that let you monitor filesystem changes.
I'm myself using linux so I'm familiar with inotify. Doing a script that compresses your files would be easy enough that I could do it for you from a feasible price.
For Mac OSX you can use FSEvents to get a same effect. Though you'll need to do it yourself.
If you wonder how to do this on windows.. Well nobody in his full senses would be using that system for software development.
If you use some eclipse-like IDE, you can create and set builder for this.
But your project should also has "Build Automatically" checkbox.
Fileconveyor is a Python script that watches and processes files, and can even upload them automatically.
I'd recommend checking out the website to see if it's something that might help you: http://fileconveyor.org/

Categories

Resources