LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   PHP exec(), passthru() and system() not working at all? Ubuntu LTS LAMP (https://www.linuxquestions.org/questions/linux-newbie-8/php-exec-passthru-and-system-not-working-at-all-ubuntu-lts-lamp-4175517422/)

wh33t 09-04-2014 10:33 PM

PHP exec(), passthru() and system() not working at all? Ubuntu LTS LAMP
 
Hey LQ,

For some reason I can't seem to get exec, passthru or system to run a specific command. It will run trivial commands such as "ls -l" but it will not run

Code:

ssh -p 223 admin@server.com "mysqldump -u admin_db -pPassword databaseName | gzip -9 > /home/admin/sql-current/sql-current.gz"
I've edited the command to not contain the actual domain or password of course. This command works correctly when run from PuTTY but it doesn't seem to do anything at all when run from a PHP script. Nor does it cause any kind of errors. I even went through the trouble of ensuring that errors were being correctly logged in /var/logs/php_errors.log and the above three php commands don't trigger an error.

Any ideas on what's going on here?

j-ray 09-05-2014 06:50 AM

Is the PHP script run by web user or from command line?

jpollard 09-05-2014 08:18 AM

What error are you getting?

wh33t 09-05-2014 01:07 PM

Quote:

Originally Posted by j-ray (Post 5232670)
Is the PHP script run by web user or from command line?

The end goal is to be able to click a link on a webpage and have it run, as of right now it doesnt work from the web or by running it as a php command from the console.

wh33t 09-05-2014 01:08 PM

Quote:

Originally Posted by jpollard (Post 5232725)
What error are you getting?

There is no errors. Running over the web it doesnt error, nor from the command line when run as a php command. Ive tested that the server is reporting and displaying errors and logging errors in /var/logs/php_errors.log and no errors are visible.

wh33t 09-05-2014 08:13 PM

Anyone! Please help!

astrogeek 09-05-2014 08:49 PM

What are you expecting it to do?

This will not return any message to the php so you will see nothing on that end.

Are you saying that it does not produce the gzipped dump file?

Just for fun, give it an invalid database name or user - something that will produce a mysql error - and see if that comes back to the php end (echo it out or capture to a variable).

wh33t 09-05-2014 09:45 PM

Quote:

Originally Posted by astrogeek (Post 5233098)
What are you expecting it to do?

This will not return any message to the php so you will see nothing on that end.

Are you saying that it does not produce the gzipped dump file?

Just for fun, give it an invalid database name or user - something that will produce a mysql error - and see if that comes back to the php end (echo it out or capture to a variable).

It does not produce the gzipped dump file. Yet the command is sound when run from the actual command line.

I gave it an incorrect databasename and user and it didn't error. It's like it's failing before it even tries.

I'll repeat, when doing something like
PHP Code:

passthru("ls -l"

it will correctly output something like

Code:

total 4 -rw-rw-r-- 1 wh33t wh33t 24 Sep 5 19:43 databasebackup.php

astrogeek 09-05-2014 10:13 PM

Try something trivial via ssh, like...

Code:

$var = exec('ssh -p 223 admin@server.com "ls -l"');
echo "Length: ".strlen($var);
echo $var;

...(interrupted whil answering...)...

exec will only return the last line of output, but if you connect you will see something, otherwise not.

wh33t 09-05-2014 10:54 PM

Quote:

Originally Posted by astrogeek (Post 5233123)
Try something trivial via ssh, like...

Code:

$var = exec('ssh -p 223 admin@server.com "ls -l"');
echo "Length: ".strlen($var);
echo $var;

...(interrupted whil answering...)...

exec will only return the last line of output, but if you connect you will see something, otherwise not.

It turns out exec, passthru and system do not permit SSH stuff. Instead you are supposed to use a prebuilt SSH system (http://php.net/manual/en/book.ssh2.php) I've got it working now. Thanks to all who tried to help me. I totally appreciate it.

astrogeek 09-05-2014 10:57 PM

Quote:

Originally Posted by wh33t (Post 5233140)
It turns out exec, passthru and system do not permit SSH stuff. Instead you are supposed to use a prebuilt SSH system (http://php.net/manual/en/book.ssh2.php) I've got it working now. Thanks to all who tried to help me. I totally appreciate it.

Hmmm... I set up a test case here using ssh inside exec which worked, but am not a PHP expert and did not know it had ssh features built in either.

Anyway, glad you got it working!

wh33t 09-06-2014 12:21 AM

Quote:

Originally Posted by astrogeek (Post 5233141)
Hmmm... I set up a test case here using ssh inside exec which worked, but am not a PHP expert and did not know it had ssh features built in either.

Anyway, glad you got it working!

Really? Can you post me your code?

astrogeek 09-06-2014 12:37 AM

Quote:

Originally Posted by wh33t (Post 5233161)
Really? Can you post me your code?

Sure... (with details obfuscated)

Code:

<?php
$var = exec('ssh -oPort=xxxxx user@host "ls -l"');
echo "Length: ".strlen($var)."\n";
echo $var."\n";
?>

And the response is...

Code:

Length: 58
-rw-r--r--  1 user users        240 Mar 21 15:46 ytref

I also ran a quoted mysqldump piped thru a gzip and redirected to a file similar to yours... works fine.

One difference - I ran mine from a shell and this user has a password login.

I was wondering if the fact that you hit if from a web server might not cause it to fail due to not having the user's fingerprint because it would be running as the webserver user.

wh33t 09-06-2014 01:11 AM

Quote:

Originally Posted by astrogeek (Post 5233164)
I was wondering if the fact that you hit if from a web server might not cause it to fail due to not having the user's fingerprint because it would be running as the webserver user.

That probably has something to do with it. Good to know. Thanks for sharing.

j-ray 09-06-2014 05:03 AM

It might be a solution to execute the ssh command with sudo and set a user for the execution in the sudoers file with visudo. You can set there that the command does not need a pssword prompt. Regarding security you run a high risk if you make database dumps availble thru a web interface.


All times are GMT -5. The time now is 04:03 AM.