Is there a way to execute a bash command when I click on a text in a browser?
The web page is on computer A, the browser is on computer B : I want to execute the code on computer B, e.g hsetroot to change the wallpaper when clicking an image.
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.
run("whatever command on machine B"); ?
PHP can change content of textfiles cross the browser for example in /var/www/.../folder/mytextfie.txt You have only to get the string from that file, whitch can be a command or even command to start a script file.
a cronjob can run to check wether that textfile contains any executable.
#!/usr/bin/env bash
echo $(cat /var/www/.../folder/commandsperline.txt)
#or
while read line
do
echo "$(${line})"
done< "/var/www/.../folder/commandsperline.txt"
context of that textfile:
#!/bin bash
#you need to declare absolute path
# chmod 755 /home/user/*.sh -to make executabble all scripts there
/home/user/backup.sh
/home/user/anyscript.sh
With te while loop it is possible to execute commands from that text-file.
Yes.. it is possible with neutralinojs.
You can use Neutralino.os.runCommand(). But in your scenario Neutralino cloud mode can be used.