admin管理员组文章数量:1336380
Due to an architecture I must work with that likely breaks a lot of very good software design rules, I need to send a message from some javascript code running in a web browser to a windows batch file on the same machine. The operating systems is Windows Vista or later. The browsers being used are primarily Chrome and Firefox. jQuery is also being used with the javascript.
The browser is connected to the internet and their is a server involved, so I could relay the message to the server, and then to the batch file. Right now I have a batch file that runs every minute or so that could theoretically query the server for any messages. Other than that, I don't have any good ideas.
Additionally, this is a "closed" system. The client browser, client system, and server are under my plete control. It is not a situation where the general public is running the Javascript in their browser. The the client puter can be manipulated to be able to receive the message.
What is a good way to send this message?
Due to an architecture I must work with that likely breaks a lot of very good software design rules, I need to send a message from some javascript code running in a web browser to a windows batch file on the same machine. The operating systems is Windows Vista or later. The browsers being used are primarily Chrome and Firefox. jQuery is also being used with the javascript.
The browser is connected to the internet and their is a server involved, so I could relay the message to the server, and then to the batch file. Right now I have a batch file that runs every minute or so that could theoretically query the server for any messages. Other than that, I don't have any good ideas.
Additionally, this is a "closed" system. The client browser, client system, and server are under my plete control. It is not a situation where the general public is running the Javascript in their browser. The the client puter can be manipulated to be able to receive the message.
What is a good way to send this message?
Share edited Apr 12, 2012 at 15:04 Chris Dutrow asked Apr 12, 2012 at 14:39 Chris DutrowChris Dutrow 50.4k67 gold badges195 silver badges262 bronze badges 3- Could you clarify what exactly you want to acplish. You can send message to server using $.ajax, then can ssh from server to the Windows machine and execute any program on mand line. Instead of ssh you can write a simple service that would listen on a socket. – Alexey Lebedev Commented Apr 12, 2012 at 14:47
- 1 You could make a firefox addon this does this, but that will take a lot of work. – Larry Battle Commented Apr 12, 2012 at 14:50
- 1 Or you can have a local HTTP server (very easy using Bottle.py), and send message using ajax directly to it. No remote server would be involved. – Alexey Lebedev Commented Apr 12, 2012 at 14:55
4 Answers
Reset to default 51. Local HTTP server
From JavaScript, send AJAX request containing your mand to local HTTP server. All modern browsers support cross-domain AJAX. The server can pass mand as a parameter to the batch file. Make sure that the server is not accessible from network.
The advantage of this approach is that you can send response from mand back to JavaScript.
JavaScript:
$.post('http://localhost:8080', {mand: '...'});
Bottle.py server:
#!/usr/bin/python from bottle import run, route, request, response import subprocess @route('/', method='POST') def index(): mand = request.POST['mand'] result = subprocess.check_output(['batchfile.bat', mand], shell=True) response.set_header('Access-Control-Allow-Origin', '*') return result run(host='localhost', port=8080, reloader=True)
Or PHP under Apache/nginx:
$result = shell_exec("batchfile.bat " . escapeshellarg($_POST['mand']));
Or daemon in PHP:
For extra fun it has full shell access and can run mands asynchronously, which allows to launch desktop applications without blocking. It doesn't need Apache.
Start:
php.exe -f batrunner_daemon.php
Use:
$.get('http://localhost:33333/ping -h', function(response) { console.log(response) });
$host = '127.0.0.1'; $port = 33333; $async = false; // enable to run mands without blocking the server (useful for GUI applications) $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $open = @socket_bind($socket, $host, $port); if ($open) { echo "\nListening on http://{$host}:{$port}"; } else { echo "\nError. Port {$port} in use"; exit; } socket_listen($socket, 1); while(1) { $connection = socket_accept($socket); $http_request = socket_read($connection, 32768); $mand = rawurldecode(substr(strstr($http_request, "\r\n", true), 5, -9)); echo "\nRunning: {$mand}..."; if ($async) { $response = pclose(popen('start "" /B ' . $mand, 'r')) ? 'error' : 'success'; } else { $response = shell_exec($mand); } echo " Done."; socket_write($connection, implode("\r\n", array( 'HTTP/1.1 200 OK', 'Content-Type: text/plain; charset=UTF-8', 'Connection: close', 'Content-Length: ' . strlen($response), 'Access-Control-Allow-Origin: *', "\r\n" . $response ))); socket_close($connection); }
To keep it simple, it doesn't have proper error handling and allows any site to execute anything on your puter, use at your own risk.
Access-Control-Allow-Origin: *
means that any site can receive response from shell. Without this header they obviously still can run mands, but can't receive response.
2. URI Scheme
I experimented with @mamdrood's idea, and it turned out to be very simple, much easier than HTTP server.
From HTML, you'll be able to call the batch file like this:
<a href="batrunner://somemand">Wipe all files</a>
And here's how the batch file in
C:\mybatfile.bat
can parse mands:@echo off SET mand=%1 SET mand=%mand:batrunner://=% echo %mand%
I'm not very familiar with batch files, and my mand name parsing code can be vulnerable. If you know any better please update this answer.
To associate your batch file with
batrunner://
protocol createbatrunner.reg
and run it once. SubstituteC:\\mybatfile.bat
for your script, path should be escaped.Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\batrunner] "URL Protocol"="" [HKEY_CLASSES_ROOT\batrunner\shell] [HKEY_CLASSES_ROOT\batrunner\shell\open] [HKEY_CLASSES_ROOT\batrunner\shell\open\mand] @="C:\\mybatfile.bat %1"
You can also create a little windows application, and use a URI scheme to connect the information, just like the BitTorrent magnets or the Skype callto.
As far as I know, JavaScript running in a browser has no way to municate with the local file system. If pages on the web could read and write files on your system it would be very, very bad for everyone. :)
If you need to generate a batch file, you could output the file from the server and let the user download it locally and run it, but JavaScript is not going to be of any assistance here.
I don't think you're going to find a "out of the box" way to trigger a local batch file with javascript. I'm sure the plugin approach will have been advocated by the time I finish posting this, but there's a lot of development overhead attached to that. Chrome also has some fancy stuff with --enable-local-file-access switches, but there are a lot of caveats attached to that, as well. (just throwing those out there as options to explore).
You might consider just providing a link that downloads the .bat file to the box and executing from there.
EDIT I may have spoken too soon. If you're using IE, it looks like a js/ActiveX solution can do what you need. Taken from here.
本文标签: Send a message from Javascript running in a browser to a windows batch fileStack Overflow
版权声明:本文标题:Send a message from Javascript running in a browser to a windows batch file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742266602a2443515.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论