How to Run a .Sh File from PHP

How to execute .sh file with php via website

in your sh add as first line

 #!/bin/sh

then call in php like this

    if(isset($_POST['password']) && isset($_POST['username'])) {
shell_exec('./create_user.sh '. $_POST['password'] . ' ' . $_POST['username']);
} else {
echo 'You must send user and pass';
}

And give to the web server user write/execute permissions.

Executing a .sh file with php

You are not posting anything, for that you'll need a form, i.e.:

file.php

<html>
<body>
<?php
if (isset($_POST['Submit1'])) {
echo shell_exec('sh /home/administrator/Desktop/Helloworld.sh');
}
?>
<form action="file.php" method="post">
<input Name= "Submit1" type="submit">
</form>

</body>
</html>

Also, make sure the file /home/administrator/Desktop/Helloworld.sh is executable, i.e.:

chmod +x /home/administrator/Desktop/Helloworld.sh

Run a .sh script in php

Here is what I did, so if it doesn't work for you then you may have some problem with WAMP or something. I might be wrong, but I don't think you're on a box that has real bash.

<?php
passthru(__DIR__ . '/lame.sh');

And the sh file (requires at least PHP v5.5 for the password_hash function):

#!/bin/bash
PASSWORD = 'kookoopapa1'
PHP=`which php`
PASSWORD=`$PHP -r "echo password_hash( '$PASSWORD', PASSWORD_BCRYPT, ['cost' => 11] );"`
printf "<script type=\"text/javascript\"> alert(\"$PASSWORD\"); </script>\n"

Give it a shot. There's also a chance that your bash is not really at /bin/bash. In your terminal type "which bash" and see what it says. Modify your shebang as needed.

Executing a shell script from a PHP script

I would have a directory somewhere called scripts under the WWW folder so that it's not reachable from the web but is reachable by PHP.

e.g. /var/www/scripts/testscript

Make sure the user/group for your testscript is the same as your webfiles. For instance if your client.php is owned by apache:apache, change the bash script to the same user/group using chown. You can find out what your client.php and web files are owned by doing ls -al.

Then run

<?php
$message=shell_exec("/var/www/scripts/testscript 2>&1");
print_r($message);
?>

EDIT:

If you really want to run a file as root from a webserver you can try this binary wrapper below. Check out this solution for the same thing you want to do.

Execute root commands via PHP



Related Topics



Leave a reply



Submit