3

I am not able to call a shell script program from javascript(for example if someone will click a button in webpage then a script will run and will throw some output)???

2

Because of security concerns, you cannot do this like you can e.g. call a binary program from a shell script, some program from Python using subprocess.check_output() or any of the other mechanisms in various programming languages.

If you want this kind of functionality, the way to go is to:

  1. run a small webserver local, i.e. on the machine
  2. allow javascript to access this (some newer versions browsers block certain calls like this)
  3. have the click of the button invoke some javascript that then accesses the local webserver to carry out the function that you need.

I have used this mechanism to allow printing, on the local machine, of multiple selected Word documents. The submit of the list of documents is redirected to the local webserver (using XML-RPC) that then retrieves the documents, and starts Word in batch mode to print them all.

| improve this answer | |
0

Here is an (untested) example using Javascript and PHP and the methodology explained by Anthon. Do not focus on syntax or whether it works or not, you can fix that later. Notice the pater with strong emphasis on data validation.

Javascript:

if (validate()) { // Preliminary data check to preven unecessary request
   $.ajax(
      '/path/to/your-script', { // the URL where the php script is hosted
         'action': 'update', // variables passed to the server
         'id': '123',
         'value': 'New Value'
      }, function (response) { // server response
       if (typeof(response.success) == 'number' && response.success) {
         }
      }, 'json' // data format
   );

}

A Rudimentary PHP Template:

 // Make sure that the POST is done via ajax
 if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])
       && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
   ) {
      // Template for multiple commands.
      switch ($_POST['action']) { // Catch all commands
          case 'update':
             // Make sure to clean up the value. Lookup tutorials on Google
             $id = sanitize($_POST['id'];
             $value = sanitize($_POST['value'];

             // Although sanitized make sure that the values pass certain
             // criteria such as duplicates, data type, user privileges etc
             if (validate($id, $value) {
                shell_exec("your '" . $id . "' '" . $value . "'";
             }
             break;
          // If we do not know what this is we can throw an exception
          default:
             throw new Exception ('Unknown Request');
      }
      // This is just an acknowledgement that the command executed. 
      // More validation and try catch constructs are highly recommended.
      echo json_encode([
              'success' => 1
           ]);

   }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.