John Davidson

IIS + PHP exec to run Linux commands with WSL not working

0 comments
Message:


Configuration



  • Windows 11

  • IIS 10.0.22000.1

  • PHP 5.6.40 (I know, it's old, but I don't know where my script will be executed, so I went broad)

  • WSL installed


Context


I am trying to write a runLinuxCommand that would run either on Windows (with Windows Subsystem for Linux) or on Linux (and perhaps OSX as it is Unix like).


I had some issues with exec("dir", $result, $code) returning $code ==> 1. I then changed the user running PHP in IIS to be my current logged in user (which is an administrator). I know, this is not ideal, but for now, I want to reach my goal. I'll try to reduce rights afterward. And... it worked!


Issue


So, I tried to execute exec("wsl date", $result, $code), but no luck, still an exit code of 1. Yes, opening a command prompt and executing wsl date does work. Either with the logged in user or as administrator.


The only thing I can see for now is it takes too much time to initialize WSL, but quite not sure at all.


Attempts


Attempt #1


The one in the issue section


Attempt #2


exec('C:\\Windows\\System32\\wsl.exe date', $result, $code);


Same result.


Attempt #3


exec('start /B /WAIT C:\Windows\System32\wsl.exe date', $result, $code);


Hanging... never getting through this line.


Attempt #4


exec('start /B /WAIT cmd /C C:\\Windows\\System32\\wsl.exe date', $result, $code);


No error. No result / output.


Attempt #5


$WshShell = new \com("WScript.Shell");
$oExec = $WshShell->Exec("C:\\Windows\\System32\\wsl.exe date");
$out = $oExec->StdOut->ReadLine();

Now I have a specific error message when running the second line: File not found. I can assure you that C:\Windows\System32\wsl.exe exists.


I already confirmed using $oExec = $WshShell->Run("whoami", 0 , true); as second line that the user is the one I am logged in (an administrator account from which I can test using a terminal).


Aside


I searched a lot about it, and I found nothing (well, everything I found was not specific).



My Answer:
) not working with WSL, so I tried using the following code snippet:

php
function runLinuxCommand($command) {
$output = shell_exec('bash -c "' . $command . '"');
return $output;
}

echo runLinuxCommand('ls -la');


However, this code snippet did not work as expected. It seems that the `shell_exec` function does not work properly with WSL.

One possible solution is to use the `wsl` command to run Linux commands in WSL. Here is an updated version of the `runLinuxCommand` function that uses the `wsl` command:

php
function runLinuxCommand($command) {
$output = shell_exec('wsl ' . $command);
return $output;
}

echo runLinuxCommand('ls -la');


This should work with WSL and allow you to run Linux commands from PHP on Windows.

Rate this post

4 of 5 based on 9691 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us