0

On a web page I would like to have a button that launches a python script on a remote machine. Is there a way to do that in php? More specifically I would like this shell command to be executed on the remote machine:

screen -d -m ./script.py

I think it might be non-trivial because of permissions needed to execute scripts without root access.

0

First i will point out that this is unsecure approach in many levels. that being said:

If you have to be root to execute script.py then your website user has to have sudoers config. First you need to find out your website user.

Then set sudo privileges for that user so that he is able to execute script.py without password, i.e:

apache ALL=NOPASSWD: /full/path/to/your/script.py

Then add executable bit to your script.py:

chmod +x /full/path/to/your/script.py

Maybe you will have to change header of your py script to:

#! /usr/bin/python

After that in your php you can execute script with shell_exec for example:

$output = shell_exec('/full/path/to/your/script.py');
0

you could do that with following snipped but your request sounds like a security issue. It would be better if the remote host has a kind of API which you can query over HTTP

<?php
   $output = shell_exec('ssh user@remotehost script.py');
   echo "<pre>$output</pre>";
?>

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.