Not able to run shell script over ssh using php - php

I'm trying to run a shell script over ssh using php. I'm been able to achieve running a simple command but not a shell script and I've tried pretty much everything I could find over the google.
Here's the script:
<?php
use SSH2;
echo shell_exec("whoami");
?>
Same script passing the shell script won't run:
?php
use SSH2;
echo shell_exec("bash awsserver.sh");
?>

Related

php to execute python script failed

I want to execute python script in PHP but it does not work, the python script is not running. But when I directly execute the python script using cmd, the script work.
Below is my PHP code.
<?php
exec('python C:\wamp64\www\dashbot\mergeCSV.py',$output,$return);
if(!$return)
{
echo "success!";
}
else
{
echo "failed!";
}
?>
Try adding this to the first line of the python script
#!/usr/bin/env python
Make sure that the python script has the execution permission
(+x).
If you are running PHP via Apache, make sure the Apache
user(www-data) has permission to access & execute the script.
Mention the full path to python exec('full/path/to/python C:\wamp64\www\dashbot\mergeCSV.py',$output,$return);

Run a .sh script in php

I'm trying to run a .sh script in php.
Here's the sh code
php fetchdrive.php
When I run the .sh script by double clicking, it works fine, and displays the variables I've returned (an array).
I'm using wampserver to run php. Multiple php files work properly, unfortunately this one can only be run command line, so I figure why not call the command via sh in my index.php file. When I try <?php echo exec('sh C:/wamp64/www/run.sh'); ?> in my index.php file, it doesn't work. I know this because at the bottom of the php file I'm trying to execute it has alert("Hello");
Infact I don't think it's running the command at all when I try to use echo exec. All of this is because I'm trying to run a command "php fetchdrive.php" without having to manually type it in to terminal, since when I start running this server I won't have to re-run the command every time.
Any ideas?
Edit: Here's the script tag for the alert
<script type="text/javascript"> alert("Hello!"); </script>
It works on my other php files.
tl;dr I'm just trying to run a php terminal command within a website.
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.

How to automate a command with PHP in web app

I have a directory that when accessed through the terminal, I run the command 'make' then 'make install' which subsequently builds a dictionary file. I want to automate this process, which will kick off when the user selects a button on the interface.
Using PHP in my web app I want to navigate to the directory which I have done so here:
chdir('../DictionaryFolder');
Then, I thought this PHP command would run the make and make install:
exec(make);
exec(make install);
But this does nothing.
Any help will be much appreciated!
You need to write the command like the following,
<?php
$output = shell_exec('make;make install;');
echo "<pre>$output</pre>";
?>
Shell exec will do the trick by calling $output via pre tag.
Exec() try to execute PHP like eval() in JS, shel_exec execute commande like you with CLI

How to run a Node.js script from PHP with XAMPP?

I am using XAMPP on Windows 7 to run a localhost, and I'm trying to use the PHP exec function to execute a Node.js script.
I am able to successfully use the exec function to run PhantomJS scripts, but when I try to do the same thing for Node.js, it doesn't work.
He's an example of a PHP script that properly runs a PhantomJS script:
<?php
exec('/phantomjs/bin/phantomjs /phantomjs/scripts/test.js', $output);
print_r($output);
And here's a similar example of a Node.js script that outputs an empty array every time:
<?php
exec('/Program Files (x86)/nodejs/node /Program Files (x86)/nodejs/test.js', $output);
print_r($output);
I'm sure that all of my paths are correct and that the Node.js script executes correctly whenever I execute it directly from the command line, but I can't get anything to return from the Node.js script when I run it from the PHP exec command. I also tried the following script, but I still get nothing:
<?php
exec('/Program Files (x86)/nodejs/node -v', $output);
print_r($output);
Any advice on what I'm missing would be greatly appreciated. Thank you.

Run server-side js from php through exec()

I have a site running on Apache/PHP, and as a matter of performance, I wrote a javascript to do a specific task.
I have installed node.js on server, in order to run this javascript. When I call the script from the command line, it works fine. See the command below:
> node myscript.js
But I need it to run from a php page, and I am trying to do this by calling the exec() PHP function, like this:
<?php exec('node myscript.js >/dev/null/ 2>&1 &'); ?>
...but it's not working.
Am I doing something wrong? Is there another way to do what I want?
I found a way to make it work! I just wrote the full directory where node.js is installed in the exec() call. Simple as that:
<?php exec('/home/bin/node myscript.js >/dev/null/ 2>&1 &'); ?>

Categories