Jump to content

php shell execution


Go to solution Solved by EffakT,

Recommended Posts

So, I am working on my assessment for class, and I am working on a script that validates Node.js code, but it seems that it is running extremely slow on the first time the port is opened.

 

What it should do is create a js file containing the user's input,

Run the script in Node.JS,

Run a cURL on the script,

Echo the output of the cURL,

Close Node.JS (closing the open port)

Delete the user's script.

 

My php is as follows:

<?php
$a = $_GET['a'];
chdir('scripts');
$a = json_decode($a);
$time = round(microtime(true) * 1000);
$data = "";
$file = $time.'.js';
$handle = fopen($file, 'w') or die('Cannot open file:  '.$file);
foreach ($a as $b) {
$data .= $b."\r\n";
}
preg_match('/(listen\s*\((\d*)\))/', $data, $port);
$port = $port[count($port)-1];
fwrite($handle, $data);
fclose($handle);
$h = popen('"C:\Program Files\nodejs\node.exe" '.$file, 'r');
sleep(1);
exec('C:\curl '.$_SERVER['HTTP_HOST'].':'.$port, $output);
foreach($output as $out) {
echo $out."\r\n";
}
pclose($h);
unlink($file);
?>

Does anybody know of any other way to do this that would reduce this lag?
Also, it seems that the pclose($h); isn't stopping node properly.

 

Thanks

 

--EffakT

Edited by EffakT
Link to comment
https://forums.phpfreaks.com/topic/287359-php-shell-execution/
Share on other sites

Okay, I managed to get it closing node.exe, now I just need it to not lag as much on loading Node.JS, but it seems to be logging the error:

PHP Fatal error:  Maximum execution time of 30 seconds exceeded in executeScript.php on line 20

New Code:

<?php
$a = $_GET['a'];
chdir('scripts');
$a = json_decode($a);
$time = round(microtime(true) * 1000);
$data = "";
$file = $time.'.js';
$handle = fopen($file, 'w') or die('Cannot open file:  '.$file);

foreach ($a as $b) {
	$data .= $b."\r\n";
}

preg_match('/(listen\s*\((\d*)\))/', $data, $port);
$port = $port[count($port)-1];

fwrite($handle, $data);
fclose($handle);
error_log("Launching Node");
shell_exec('"C:\Program Files\nodejs\node.exe" '.$file);
error_log("Running cURL");
exec('C:\curl '.$_SERVER['HTTP_HOST'].':'.$port, $output);
error_log("killing node");
exec('taskkill /F /IM node.exe');

foreach($output as $out) {
	echo $out."\r\n";
}

unlink($file);
?>
Link to comment
https://forums.phpfreaks.com/topic/287359-php-shell-execution/#findComment-1474364
Share on other sites

  • Solution

Sweet, thanks boompa, that wasn't but problem but actually helped a lot!

 

I run the node script in the background (this doesn't slow down my script), then run a cURL and then close node, if cURL returns an error, I run node normally since it will return an error and wont execute. This is so I can get the error that node returns.

Link to comment
https://forums.phpfreaks.com/topic/287359-php-shell-execution/#findComment-1474401
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.