views:

1066

answers:

7

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?

+13  A: 

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?

pjc50
I know that this a security risk, but this is an urgent requirement to run pre-installed program on client and to be from Javascript?
Ahmed
You could have a batch file to restore drive mappings on an intranet site, for example
Andomar
You might be able to achieve this with Microsoft ClickOnce (unreliable, requires IE and a .NET application)Some more context please: where is this webpage? What is the application?
pjc50
I need to run pre-installed program on client side to.
Ahmed
pjc50 didn't mean "security risk" in the sense that it's inadvisable, but rather "security risk" in the sense that browsers will not allow it.
Matt G
+5  A: 

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!

DreamSonic
+1  A: 

Redirect the client to http://yourserver/batchfile.bat. Under some browsers, this will prompt the user to run the batch file.

Andomar
http://yourserver/batchfile.bat is server side, file to be executed is client side?
Ahmed
It would download the batchfile to the PC, and execute it there, if the user selects "Run".
Andomar
@Ahmed: what he's proposing is that you direct the browser to download the .bat file and have the user execute it.
belgariontheking
+1  A: 

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.

Dave Webb
A: 

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

Michael Kohne
So... the JavaScript asks the server to run psexec to start a program on the client? Would work I guess.
Andomar
Well, yea. And again, this only works if the server is running as a user that has appropriate permissions on the client.
Michael Kohne
A: 

The only sane idea that comes to mind is maybe use ClickOnce to deploy the app you need and possibly run it.

DreamSonic
A: 

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.

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

  2. Then make a web pages that allow the user to "register" their services with proper authentication to invoke that service (password, security key)

  3. When you want to invoke your application from web-page on the client that's already registered, make ajax call (xmlhttprequest) to your server.

  4. The server should validate the requesting IP address with registered information.

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

Sake
But how to do this from web page/ javascript?
Ahmed
Make an ajax call to your server.
Sake