How to run shell command in JavaScript - javascript

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");

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.

K6 how run node command in the setup function

Please excuse my limited K6/Javascript knowledge.
I have been given a javascript file that can be used to create a batch of users to be used within my k6 load test.
The script is currently ran from the commmand line using this command:
node helpers/createUsers.js 10
I want to run this script in the 'setup' function in K6.
Can I run node commands directly in K6 as above, or do I have to turn it into an export function and then call it?
To my knowledge, you cannot run nodeJS or any nodeJS modules directly from a k6 test script. If the script helpers/createUsers.js is using any node modules, then you should follow this. and have the script in the setup() function.

How can I run a JavaScript automatically on a command-line?

I have a simple JavaScript myScript.js with code as below;
var printer = function(someString){
console.log(someString);
}
printer("This is printed on console.");
I'm on Windows 10 and need to be able to call myScript.js on a command prompt and have the code executed without doing node myScript.js. Is there a way to set up things so that Command prompt or PowerShell can automatically call Nodejs or other JavaScript engine?
If your Windows can run normal shell scripts on the command line then you can add a shebang line to your Node script just like you do with shell scripts. For example:
#!/usr/bin/env node
// your node code here
Also you can try to configure Node to be invoked for all files with the .js extension but this can be a security hazard because you may execute arbitrary code just by clocking on JavaScript files that you may have downloaded on your system so I wouldn't really recommend that.
Another alternative is to make a BAT script for you Node app:
example.bat:
node example.js
example.js:
// your Node script
That you will be able to run with just:
example
on your command line.

Run cygwin script with arguments from java as background process

I am trying to run cygwin script from java code. I have used following code for same,
String[] command = {"cmd.exe","/C","Start","C:/cygwin64/bin/bash","./bin/script.sh","input_file.mkv","I:/video/output.avi"};
Runtime.getRuntime().exec(command);
it works fine but, instead of calling script from "cmd" as described above. Can we run cygwin bash script directly? I have tried following code but it is not working
String[] command = {"C:/cygwin64/bin/bash","./bin/script.sh","input_file.mkv","I:/video/output.avi"};
Runtime.getRuntime().exec(command);
I am not getting what I am doing wrong. Also I want script to run as background process, so what can we do for same?

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

Categories

Resources