如何执行linux bash命令时点击按钮?命令内容基于用户输入



HTML:这里我有两个输入



<input id="range3"  type="range" min="0" max="255" value="0" />
<input id="num3" min="0" max="255" type="number" value="0" />

<input id="range4"  type="range" min="0" max="255" value="0" />
<input id="num4" min="0" max="255" type="number" value="0" />

JS:这里我得到输入的结果,并将其写为html的结果;因此,我想在linux中执行它。

// 30
var range3 = document.getElementById("range3");
var num3 = document.getElementById('num3');
range3.addEventListener('input', function (e) {
num3.value = e.target.value;
});
num3.addEventListener('input', function (e) {
range3.value = e.target.value;
});

// 40
var range4 = document.getElementById("range4");
var num4 = document.getElementById('num4');
range4.addEventListener('input', function (e) {
num4.value = e.target.value;
});
num4.addEventListener('input', function (e) {
range4.value = e.target.value;
});
function execute(){                            

document.getElementById("result").innerHTML =  "asusctl fan-curve -m " + mode.value + " -D " + "30c:"+ num3.value + ",40C:" +  num4.value + "  -e true -f "+ unit.value ;

}

我想让execute函数的结果作为linux命令运行

您将需要使用AJAX向服务器发送信息。然后服务器可以运行bash命令。

function sendCommand(command) { 
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "sendCommand.php?q=" + command, true);
xmlhttp.send();
}

PHP服务器示例

sendCommand.php

<?php
$cmd= $_GET['q'];
echo shell_exec($cmd);

变化:"asusctl fan-curve -m " + mode.value + " -D " + "30c:"+ num3.value + ",40C:" + num4.value + " -e true -f "+ unit.value;

To:sendCommand("asusctl fan-curve -m " + mode.value + " -D " + "30c:"+ num3.value + ",40C:" + num4.value + " -e true -f "+ unit.value);

最新更新