Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

79
Views
How to execute a linux bash command when clicked on button? The command content is based on user input

HTML: Here I have two inputs


           
           <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: Here I get the result of the inputs and write it as html the result ; inplace of writing it as an result I want to execute it in 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 ;
  
}

I want that that the result of execute function to be run as a linux command

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

You will need to use AJAX to send information to the server. The server can possibly then run the bash command.

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();
}

Example for a PHP server

sendCommand.php

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

Change: "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);

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs