Execute a shell command upon a click event in a web page

Not really, no. That is the mother of all security holes. You are asking if you can set up a webpage that executes arbitrary commands on the client's machine. What if I set up a webpage that runs this command:

rm -rf ~/

That would delete all files in your $HOME. In fact, there was recently an uproar when a bug was discovered that allowed this to happen. One of the possible attack vectors was tricking the client (computer B in your case) into running a bash command.

So no, you can't execute arbitrary code on your local machine through a web browser. Not without somehow logging in first. You can run JavaScript or similar language commands but they won't have access to your user's session.


I solve it using a nodeJS server. (not clean/final code but its working)

Computer A : (server)

function change_wallpaper(image){
    var objReq = new XMLHttpRequest();
    objReq.open("GET", "http://localhost:8888" + "?image=" + image, false);
    objReq.send(null);
}
<img src="./img/1.jpeg" onclick="change_wallpaper(this.src);" />
<img src="./img/2.jpeg" onclick="change_wallpaper(this.src);" />

Computer B : (client) file called server.js ans executed with nodejs server.js

var http = require("http");
var sys = require('sys')
var exec = require('child_process').exec;
var url = require("url");

function onRequest(request, response) {
    var params = url.parse(request.url,true).query;
    function puts(error, stdout, stderr) {sys.puts(stdout)}
    exec("/usr/bin/feh --bg-center " + params.image, puts);
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Wallpaper');
}

http.createServer(onRequest).listen(8888);

You cannot do that in general, but in a defined environment, where you can control the client machine B, you can propose that machine B runs a setroot service, you can simply write on your own with nodejs or golang for example and talk to that service when you receive the event. But in any case you don't have any chance (or at least you never should have a chance) if you don't install some extra service on machine B.