5

Inside my automation.php controller, I have the following function:

public function deploy_test() {

      echo json_encode(system("python --version"));
 }

When the user wants to deploy a test, by clicking a test button in the webpage, he would be able to accomplish such a task. However, when I click the test button, my output is:

""

Meanwhile, when I execute the same function with the command:

public function deploy_test() {

    echo json_encode(system("ls -l"));
}

I'm getting:

total 32
drwxr-xr-x. 15 philippe philippe 4096 Mar  4 16:48 application
drwxrwxr-x.  2 philippe philippe 4096 Mar  4 17:28 css
-rw-r--r--.  1 philippe philippe 6357 Jan 30 11:53 index.php
drwxrwxr-x.  2 philippe philippe 4096 Feb 27 15:38 js
-rw-r--r--.  1 philippe philippe 2496 Jan 30 11:53 license.txt
drwxr-xr-x.  8 philippe philippe 4096 Jan 30 11:53 system
drwxr-xr-x. 12 philippe philippe 4096 Jan 30 11:53 user_guide

Could someone please help me to get that straighten out?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
cybertextron
  • 10,547
  • 28
  • 104
  • 208
  • 1
    does the python command work when typed directly into the command line? – Jeemusu Mar 05 '13 at 06:36
  • @Jeemusu It does, and it also works when I execute the command above from a simple `test.php` file. – cybertextron Mar 05 '13 at 17:06
  • Are the test.php and codeigniter code being run on the same server? – Jeemusu Mar 07 '13 at 07:11
  • @Jeemusu Yes, they are – cybertextron Mar 07 '13 at 17:24
  • what does `python -c "print(1); exit(10)"` and `env python --version` do? – User Mar 10 '13 at 10:31
  • Check if the user/group from the webserver has permissions to execute python commands. Also try using the full path for python. It shouldn't be a limitation of CodeIgniter, but I'll try to test something and comment again. Good luck with that. – juanrossi Mar 12 '13 at 19:33
  • echo json_encode(shell_exec("python --version")); or echo json_encode(exec("python --version")); What does that give you? One of those should return the json encoded output. – rexposadas Mar 15 '13 at 06:45
  • It's a user/group permission problem. I tried it in CI and executing it from the commandline works, but from the browser doesn't because it's a different user who isn't allowed to execute python from the commandline – unicorn80 Mar 15 '13 at 10:33

5 Answers5

4

The problem is not with your code or PHP.

The problem is with your permissions.

php uses permissions which are set in the env-vars of apache.

Which is ideally set as :

User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

under your apache2 / httpd conf file.

For example:

Try running:

<?= `whoami` ?>

via your shell and via your browser.

Your browser will probably say www-data and shell will say your username or if you are on AWS - default, you would get root

You do not have to use system() , use exec() instead.

We should be close as :

echo json_encode(exec("python --version"));

Performing operations will require you to have correct User and Groups set.

Look up for : In the shell, what does " 2>&1 " mean?

So your code should be :

echo json_encode(exec("python --version 2>&1"));

Hope it helps!

Community
  • 1
  • 1
Kautil
  • 1,321
  • 9
  • 13
1

This works fine for my production server

public function deploy_test() {
    echo json_encode(system("python --version 2>&1"));
}

with the output

Python 2.7.3
"Python 2.7.3"

Output of the unix command printed twice as system() itself outputs the result to browser. So exec() can be used in the place of system to avoid this.

public function deploy_test() {
    echo json_encode(exec("python --version 2>&1"));
}

which outputs

"Python 2.7.3"

as expected.

Cyril
  • 506
  • 14
  • 23
0

I suspect it is not in the path. Try:

Type the full path to the python command (such as /usr/bin/python --version)

  1. Find out with the command which, 'which python'

  2. try executing your script from the command line, 'php script.php' => sometimes the web server sets up things differently

  3. make sure the error displaying is enabled with

    ini_set('display_errors',1);

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
Youn Elan
  • 2,379
  • 3
  • 23
  • 32
  • a couple of things: I created a mock `test.php` file, and I executed the program, and it worked as supposed. Then when I executed my `script.py` the results were the expected. The problem appears to be when I try to execute it from `codeigniter`. – cybertextron Mar 05 '13 at 15:43
0

system returns only last line. system

public function deploy_test() {

      system("python --version", $out);
      echo json_encode(implode($out));
 }
Peinguin
  • 85
  • 5
  • it returns `0` only. and `$out` needs to be an array in order to work with `implode` ( it is not ). – cybertextron Mar 05 '13 at 17:09
  • Sorry. Python -v returns output into second stream. You have to use $retval = exec("/usr/bin/python --version 2>&1", $out); var_dump($retval); – Peinguin Mar 06 '13 at 07:06
0

It's a bit of hack, but you can find out the version number with 1 decimal even if you aren't allowed to execute python (which is the case with the 'user' CI).

//find python path
exec("which python", $path);

//show all subdirs in python    
exec("ls -l ".$path[0]."*", $output);
$output = implode("\n", $output);

//preg match on version numbers    
preg_match_all("#python(\d+(?:\.\d{1,2})?)#", $output , $matches);
$installed_versions = $matches[1];

//sort in reversing order
$versions_sorted_desc = array_reverse($installed_versions);

//latest version is element 0
$latest_version = $versions_sorted_desc[0];

echo $latest_version;
unicorn80
  • 1,107
  • 2
  • 9
  • 15