LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-04-2014, 11:33 PM   #1
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 672

Rep: Reputation: 46
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?
 
Old 09-05-2014, 07:50 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,534

Rep: Reputation: 132Reputation: 132
Is the PHP script run by web user or from command line?
 
Old 09-05-2014, 09:18 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,655

Rep: Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256
What error are you getting?
 
Old 09-05-2014, 02:07 PM   #4
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 672

Original Poster
Rep: Reputation: 46
Quote:
Originally Posted by j-ray View Post
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.
 
Old 09-05-2014, 02:08 PM   #5
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 672

Original Poster
Rep: Reputation: 46
Quote:
Originally Posted by jpollard View Post
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.
 
Old 09-05-2014, 09:13 PM   #6
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 672

Original Poster
Rep: Reputation: 46
Anyone! Please help!
 
Old 09-05-2014, 09:49 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,033
Blog Entries: 1

Rep: Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119
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).
 
1 members found this post helpful.
Old 09-05-2014, 10:45 PM   #8
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 672

Original Poster
Rep: Reputation: 46
Quote:
Originally Posted by astrogeek View Post
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

Last edited by wh33t; 09-05-2014 at 10:59 PM.
 
Old 09-05-2014, 11:13 PM   #9
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,033
Blog Entries: 1

Rep: Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119
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.

Last edited by astrogeek; 09-05-2014 at 11:37 PM.
 
Old 09-05-2014, 11:54 PM   #10
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 672

Original Poster
Rep: Reputation: 46
Quote:
Originally Posted by astrogeek View Post
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.
 
Old 09-05-2014, 11:57 PM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,033
Blog Entries: 1

Rep: Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119
Quote:
Originally Posted by wh33t View Post
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!
 
Old 09-06-2014, 01:21 AM   #12
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 672

Original Poster
Rep: Reputation: 46
Quote:
Originally Posted by astrogeek View Post
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?
 
Old 09-06-2014, 01:37 AM   #13
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,033
Blog Entries: 1

Rep: Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119Reputation: 2119
Quote:
Originally Posted by wh33t View Post
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.
 
Old 09-06-2014, 02:11 AM   #14
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 672

Original Poster
Rep: Reputation: 46
Quote:
Originally Posted by astrogeek View Post
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.
 
Old 09-06-2014, 06:03 AM   #15
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,534

Rep: Reputation: 132Reputation: 132
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How to Install and Configure LAMP in Ubuntu Server 14.04 LTS LXer Syndicated Linux News 0 06-11-2014 02:32 AM
LXer: Installing Apache2 With PHP5 And MySQL Support On Ubuntu 12.04 LTS (LAMP) LXer Syndicated Linux News 0 05-14-2012 11:50 AM
exec() in php script not working magicalshashank31 Programming 1 12-24-2011 11:31 AM
LXer: How To Build A LAMP Server With Ubuntu 6.06 LTS LXer Syndicated Linux News 1 06-06-2006 10:25 AM
PHP EXEC() and PASSTHRU() Security crazyace Linux - Security 2 12-29-2002 08:15 AM


All times are GMT -5. The time now is 12:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration