0

I have a problem running a command from a PHP script. The command I am trying to run is

echo y | plink -ssh -N -D 9999 admin@1.2.3.4 -pw admin -v

The thing is that the command runs but the script freezes until the execution of plink command, which I don't want. I also tried (running in background) this :

START /MIN "cmd.exe" /C "plink -ssh -N -D 9999 admin@1.2.3.4 -pw admin"

and I see minimized plink running, and as soon as I close it, the script continues.

I also tried:

START /B /MIN "cmd.exe" /C "plink -ssh -N -D 9999 admin@1.2.3.4 -pw admin"

and it does the same thing, but is showing the output in the PHP script.

this is the function :

function create_tunnel($ip,$user,$pass,$port)
{
    exec('START /min cmd /c "echo y | plink -ssh -N -D '.$port.' '.$user.'@'.$ip.' -pw '.$pass.' -v" > nul');
}

What must I do to run this command and let the PHP script continue execution? In linux this would be very simple, I would just use screen command.

Thanks.

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
Damian
  • 761
  • 1
  • 9
  • 26

1 Answers1

0

Try Symfony Process component:

$process = new Process('ls -lsa');
$process->start();

while ($process->isRunning()) {
    // waiting for process to finish
}

echo $process->getOutput();
ankhzet
  • 2,517
  • 1
  • 24
  • 31
  • do i need to inculde a special class or something? or it is in PHP libraries? also i am using WINDOWS if i was in LINUX i would just use screen – Damian Sep 29 '15 at 21:58
  • It's a tiny standalone part of well known php framework Symfony. It's crossplatform, obviously, and well tested. You can install it via Composer or download from official repo, then include `vendor/autoload.php` file in your's script and it will be ready for usage. – ankhzet Sep 29 '15 at 22:05
  • Also, you can peep at it's implementation for revealing details of async command execution. There `popen` php function used instead of just `exec`, afaik. – ankhzet Sep 29 '15 at 22:06
  • `pclose(popen('START /b /min cmd /c "echo y | plink -ssh -N -D '.$port.' '.$user.'@'.$ip.' -pw '.$pass.' -v" > nul',"r"));` ??? this?? apparently works ... don't know why ... – Damian Sep 29 '15 at 22:53
  • `popen` just opens started process handle. `exec` also waits for process output, so, it blocks script until started process terminates. in order to retrive executed process output when using `popen` you must just read it as from regular file with `fread`, manually. So, basically, `exec` internally uses `popen` and then waits for output. – ankhzet Sep 29 '15 at 23:21