douweng1904 2017-11-12 19:47
浏览 181

PHP exec不返回外部程序的输出

I've the following program:

function execute_query($ip, $username, $password, $query){
    $runCMD ="python run_query.py " . escapeshellarg($ip). " ". escapeshellarg($username) . " " . escapeshellarg($password)
        .  ' ' . escapeshellarg($query);
    $output = [];
    exec ($runCMD, $output);
    print_r ($output);
    //return (implode("
", $output));
}

However the array $output is always empty, the program is returning the result correctly, when executed manually from CMD. I've no idea what to do. I tried so many things that I even don't recall all of them. Here are a few:

1.I disable UAC
2.I tried using shell_exec
3.I tried appending powershell in front of the command 'powershell' . $runCmd
4.I tried appending '2>&1' at the end of the command
5.I tried using proc_open, to manually open cmd.exe and execute the command

Any other ideas what I may do, or at least how to continue troubleshooting. Using Linux is not an option It seems like stderr is working. Also i tried printing 'asd' from the python script and it's working. The output from the python script is WMI query, it may contain , and \t

Here is the python script:

from subprocess import check_output
import sys

def run_query(ip, username, password, query):
    runCMD = "query.exe " + username + " " + password + ' "' + query + '" ' + ip + ''
    results = check_output(runCMD).decode("utf-8")
    results = results.split("
")
    for result in results:  
        if not result.startswith("__"):
            print(result)
if __name__ == "__main__":
    run_query(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])

UPDATE 1
query.exe is a program, which returns filtered result from WMI query, passed as argument. No idea what's inside but it's working as expected. I also tried running query.exe from the php script, the same thing no output.
UPDATE 2
I'm using php 7.1, tried php 5.6 without luck.
UPDATE 3
When I try to change the string into byte array in python I get this output when the script is manually runned:

bytearray(b'The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)')
bytearray(b'')

, but the php script returns this:

Array
(
    [0] => bytearray(b'')
)


UPDATE 4
f*** it I'm switching the whole project to python

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号