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

扶醉桌前 提交于 2019-11-26 14:47:49

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?

DreamSonic

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.

  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.

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

Dave Webb

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!