Exec() function problem

Time:2020-4-7

Source code:

 

 

 

I searched the Internet for answers

The detailed answer is:

 

<?php

exec(“lsof -i:80”,$out,$status);

Result:

var_dump($out);

array(0) { }

Echo $status; value 127

2. Through: shell exec (“id-a”); check out users and user groups

<?php

echo shell_exec(“id -a”);

Results: uid = 1002 (WWW) GID = 1002 (WWW) groups = 1002 (WWW)

 

3. Log in to Linux and give root level permission to WWW users

Modify the / etc / sudoers file

root ALL=(ALL) ALL

Add a line below

www ALL=(ALL) ALL

Execute PHP after modification, the result is meaningless, and the returned array is still empty array (0) {}

4. Modifying permissions has no effect. The online query says that the return 127 of the third parameter of exec may be related to the path, so try from the path aspect

exec(“whereis lsof”, $out);

var_dump($out);

Result:

array(1) {  [0]=>  string(50) “lsof: /usr/sbin/lsof /usr/share/man/man8/lsof.8.gz” }

File path of lsof command: / usr / SBIN / lsof

So change exec (“lsof-i: 80”, $out1, $status); to exec (“/ usr / SBIN / lsof-i: 80”, $out1, $status);

var_dump($out1);

The result was successful

The results are as follows:

array(6) { 

[0]=>  string(56) “COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME” 

[1]=>  string(67) “nginx   31706  www    6u  IPv4 165987      0t0  TCP *:http (LISTEN)”  

[2]=>  string(67) “nginx   31707  www    6u  IPv4 165987      0t0  TCP *:http (LISTEN)”  

[3]=>  string(113) “nginx   31707  www   12u  IPv4 232481      0t0  TCP localhost.localdomain:http->192.168.204.1:55893 (ESTABLISHED)”  

[4]=>  string(67) “nginx   31708  www    6u  IPv4 165987      0t0  TCP *:http (LISTEN)”  

[5]=>  string(67) “nginx   31709  www    6u  IPv4 165987      0t0  TCP *:http (LISTEN)”

 }

_______________________________________________________________________________________________________

Linux configuration:

Php.ini configuration file I did not disable exec() function security mode is also false

/The etc / sudoers file is also configured

 

 

It didn’t work. It’s tragic

 

So I did an experiment

 

Prove that my configuration above is correct!!!!

The command of the system does not need to add the full path. Generally, the command placed under / usr / bin /

But the problem is that which PHP can’t always return the value????????

 

Exec ("/ usr / local / node / bin / node - V 2 > & 1", $out, $status); there is only another way, which is more conservative