How to invoke Linux command with JavaScript - javascript

I am new in JavaScript, and now I want to run a Linux application in JavaScript and show the result in a web page. It all happens in client without a server. But for security issues or something else, JavaScript as no such interfaces. I know in Windows, it could be achieved with activeX, but how could I achieve this in Linux?
I just wanna make js+browser equal to a client application without the complex GUI design. So there is no server. Any easy way to accomplish it?

Maybe Node-webkit is what you want.
It allows you to easily create a desktop application in javascript and access "low-level" stuff so you can run commands, without the need of an http server.

Can't be done.
JavaScript cannot interact with the users system in any way.
The only thing you can do is send a request to a server, which executes the command and sends the response back.

With javascript alone can't be done. But you have at least two option to do this using javascript plus other technologies.
The first one is what Jivings sent, using Jquery to sent a post request to a scripting language at the server side.
The second way is to install Node.js. This platform runs on javascript engine, and has all the features you need from the language. After installing it just see this post that explains how to execute a command.

Related

Launch a program from the destop using jQuery or PHP [duplicate]

How can I run a batch file on the client side? An exe file? Just to open pre-installed program in client side?
[Edit]
Regarding ActiveX, I tried
var activeXObj = new ActiveXObject("Shell.Application");
activeXObj.ShellExecute("C:\\WINDOWS\\NOTEPAD.EXE", "", "", "open", "1");
but this doesn't work. Any suggestions?
From Javascript? You can't. It's a security risk. Think about it - would you want every website to be able to run programs on your PC?
You mean launch an external program thru a browser window using JavaScript? No way you can do that! That's a goddamn security black hole!
<script language="javascript" type="text/javascript">
function RunEXE(prog) {
var oShell = new ActiveXObject("WScript.Shell");
oShell.Run('"' + prog + '"', 1);
}
</script>
If you really have control on the client, then you may want to install some remote daemon service on the client side, like SSH.
PS. Invoke it through your "server-code", however.
Updated:
Don't be discouraged. You can absolutely do that in safe manner.
First you need a daemon service on the client that will handle the task of invoking your application. Personally, I'd rather build simple rpc-server as windows-service with C++ or Delphi; but many other kinds of server could also do the job (SSH, Apache, Telnet)
Then make a web pages that allow the user to "register" their services with proper authentication to invoke that service (password, security key)
When you want to invoke your application from web-page on the client that's already registered, make ajax call (xmlhttprequest) to your server.
The server should validate the requesting IP address with registered information.
Then make a remote command invokation to the client with the registered information.
There can be some networking situation that this scheme might not work. However, if you really have control on the execution environment then there always be some workarounds.
Redirect the client to http://yourserver/batchfile.bat. Under some browsers, this will prompt the user to run the batch file.
If the problem is the batch file is being displayed in the browser you need to set Content-Type and Content-Disposition in the HTTP header so the user is prompted to Save (or Run) the file rather than have the browser display it.
You won't be able to run the file without an OK from the user but this shouldn't be a problem.
Have a look at this question for a little more detail.
Basically, you can't. If you need to launch something on the client side, you'll need another mechanism altogether, presumably one with some security built in. A previous poster mentioned psexec (http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx), which will obviously only work if you have appropriate permissions on the target system, and which is completely outside the browser.
Basically, what you are asking for is a BIG, BIG, problem if you were able to do it easily.
You might look into ActiveX, but I don't know what limits there are on an ActiveX object these days (I know there ARE limits, but perhaps you can work within them).
It's not allowed directly, for security reason. Besides all other answers, there is another idea.
You can build a localhost rest service in your pre-installed program and use javascript to call it with command or data, well assuming that you write the the pre-installed program and the service is supposed to be running when you call. This solution works in some scenarios.

Javascript send string to console application

I want to execute some javascript that will send a string of text to a c++/java console application running on the same machine as the web browser
What javascript should I execute? and how should I receive the string?
As far as I know, there are two ways to transmit data to somewhere else using JavaScript. The first is using an XMLHttpRequest, the second is using a WebSocket. In both cases, the JavaScript code will establish a connection to another program, which in this case you want to be your C++/Java program.
In the first case, if you want to communicate using an XMLHttpRequest (or other libraries that use this, such as jQuery's get or post), you'd need to make sure that your C++/Java application starts a small webserver. This way, the JavaScript code can establish a connection to it and send data. I'm sure there are C++/Java libraries which you can use for this, but even if you can't it should be fairly simple to get something to work with just plain socket code. The text-based HTTP protocol that you need for this is not that difficult.
In the second case, you'd need to make sure that a WebSocket server is started in your C++/Java application. Your JavaScript code then can connect to this server and just send its data. I'm not that familiar with the WebSocket protocol but I suspect that it's slightly less trivial, so using a library for this would be a good choice.

HTML5/javascript X11 server?

We have an application which consumes a large amount of data. Currently a desktop app, but we would like to deliver it via the browser.
It doesn't make sense to me to create a web app where we need to transfer a ll the data used for the visualizations.
We're looking at RDP and some products out there that provide RDP access via a fully javascript client. They seem to work well with our app, but I've been thinking about what it would take to move off Windows.
Switching the front end so that it could run under Linux would not be trivial, but not impossible, so the main stumbling block would be delivery.
I was wondering if there are any X11 javascript servers out there, but have not found any leads.
Use xpra's builtin html5 client, it supports any application you can run on an X11 desktop.
You can use an HTML5 VNC viewer like https://github.com/kanaka/noVNC coupled with a VNC server like RealVNC
AFAIK, recent GTK has been ported to HTML5+Javascript in Gtk Broadway
And you could make your application a web application, for instance by using Wt, or by making it an HTTP server thru specialized HTTP server libraries like libonion, libmicrohttpd etc.
By using AJAX techniques (e.g. thru jquery) your application won't transmit all the display data to the browser at once (but only incrementally and only the actually shown data).
You might also consider fastcgi as a way to connect your application to some web server.
I know two, both at very infancy:
https://github.com/GothAck/javascript-x-server
and
https://github.com/ttaubert/x-server-js
Both need simple tcp-to-websockets proxy in front, but all X11 logic happen inside web page and all x11 objects exist and interact within browser (so it's not just remote framebuffer but real server)
You can ever run full Linux distribution in Web Browser, but that's require to run x86/ARM emulator and GNU/Linux inside it. It provides X server with possible web connection too.
For very simple applications you can use libgreattao toolkit and tao-network-client to connect to it. I'm the author of both project. The API isn't yet frozen, but it rather behaves stable. You can read about it here:
https://nintyfan.wordpress.com/2015/04/30/server-buildin-into-libgreattao-and-tao-network-client/
It can provide some problems with applications with a lot of data, because all elements must be send to client, when it were created, but instead we don't send full graphics(only icons is send) and user interface could be changed quickly. It also don't support mouse enter/leave/move events.
I must tell: do not download tarbar, but download version from svn.
Sounds like the easiest approach for you is to get xrdp, which is an RDP-server for X. Then you would use your RDP client to connect to it. I think Nomachine NX supports html directly now, but I'm not sure. There was talk of an html X2go-client, but I don't know anything about that either.

Execute a Application On The Server Using JavaScript

I have an application on my server that is called leaf.exe, that haves two arguments needed to run, they are: inputfile and outputfile, that will be like this example:
pnote.exe input.pnt output.txt
They are all on the same directory as my home page file(the executable and the input file). But I need that a JavaScript could run the application like that, then I want to know how could I do this.
I'm using just Apache, I don't have any language for web installed on it. My goal is to do a site using just JavaScript, without the help of anyother language than it, HTML and CSS.
You would need to make an Ajax request to the server - the server would then have a handler that would then invoke the executable with the appropriate parameters.
Without know which web server technology you are using, it's harder to give a more concrete answer (ex: ASP.NET, PHP, Ruby, etc).
EDIT: If you're talking about doing this without any kind of server side resources, then this is impossible, and for good reason. Think of the security exploits!
Any other way to this without using other languages that need to be installed on the server?
No, but you almost certainly already have languages on the server. If it's a Linux, BSD or OSX server you've got shell script; if it's a Windows server you've got JScript and VBScript via Windows Scripting Host (using a cscript.exe hashbang).
JavaScript is for Client Side of a web application, so you won't be able to directly use javaScript to access server side files. As mentioned by Tejs, you should use Ajax to make a call to server side and then use appropriate server side routine to do the task.
Even at client side, most browsers don't allow accessing of any resource( e.g files) by javaScript code.
For server side javascript in Apache you could use Sun ONE Active Server Pages, formerly known as Chili!Soft ASP. For an IIS server, javascript is plainly available as asp-language.
Look into Rhino and node.js. I dont know a lot about this, but thats a route you can use for serverside javascript.

How can I run a program or batch file on the client side?

How can I run a batch file on the client side? An exe file? Just to open pre-installed program in client side?
[Edit]
Regarding ActiveX, I tried
var activeXObj = new ActiveXObject("Shell.Application");
activeXObj.ShellExecute("C:\\WINDOWS\\NOTEPAD.EXE", "", "", "open", "1");
but this doesn't work. Any suggestions?
From Javascript? You can't. It's a security risk. Think about it - would you want every website to be able to run programs on your PC?
You mean launch an external program thru a browser window using JavaScript? No way you can do that! That's a goddamn security black hole!
<script language="javascript" type="text/javascript">
function RunEXE(prog) {
var oShell = new ActiveXObject("WScript.Shell");
oShell.Run('"' + prog + '"', 1);
}
</script>
If you really have control on the client, then you may want to install some remote daemon service on the client side, like SSH.
PS. Invoke it through your "server-code", however.
Updated:
Don't be discouraged. You can absolutely do that in safe manner.
First you need a daemon service on the client that will handle the task of invoking your application. Personally, I'd rather build simple rpc-server as windows-service with C++ or Delphi; but many other kinds of server could also do the job (SSH, Apache, Telnet)
Then make a web pages that allow the user to "register" their services with proper authentication to invoke that service (password, security key)
When you want to invoke your application from web-page on the client that's already registered, make ajax call (xmlhttprequest) to your server.
The server should validate the requesting IP address with registered information.
Then make a remote command invokation to the client with the registered information.
There can be some networking situation that this scheme might not work. However, if you really have control on the execution environment then there always be some workarounds.
Redirect the client to http://yourserver/batchfile.bat. Under some browsers, this will prompt the user to run the batch file.
If the problem is the batch file is being displayed in the browser you need to set Content-Type and Content-Disposition in the HTTP header so the user is prompted to Save (or Run) the file rather than have the browser display it.
You won't be able to run the file without an OK from the user but this shouldn't be a problem.
Have a look at this question for a little more detail.
Basically, you can't. If you need to launch something on the client side, you'll need another mechanism altogether, presumably one with some security built in. A previous poster mentioned psexec (http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx), which will obviously only work if you have appropriate permissions on the target system, and which is completely outside the browser.
Basically, what you are asking for is a BIG, BIG, problem if you were able to do it easily.
You might look into ActiveX, but I don't know what limits there are on an ActiveX object these days (I know there ARE limits, but perhaps you can work within them).
It's not allowed directly, for security reason. Besides all other answers, there is another idea.
You can build a localhost rest service in your pre-installed program and use javascript to call it with command or data, well assuming that you write the the pre-installed program and the service is supposed to be running when you call. This solution works in some scenarios.

Categories