I want to do connection between php and scilab, this my code
try {
$path = 'C:\\wamp64\\apps\\scilab-5.5.2\\bin\\Scilex.exe';
$path_script = "ea=loadfls('C:\\wamp64\\www\\scilab\\estilosaprendizaje.fls');res=evalfls([-11,11],ea); disp(res);exit;";
$command = $path . ' -nb -e "' . $path_script.'"';
echo $command;
exec($command, $output);
foreach ($output as $line) {
print_r($line);
echo "<br />";
}
} catch (Exception $e) {
echo 'Excepción capturada: ', $e->getMessage();
}
but when I run the php, it does not work, it keeps loading, I do not have error messages or anything.
In scilab my code it works.
My output in scilab
Try adding -nw to $command to lauch scilab in console mode. It may be because scilab try to launch itself in graphical mode. I dont have php nor fuzzy toolbox, so I could not test it.
Related
I want to edit cron tab daily to delete / add some jobs .. so I added cron job ( helper job ) to run php script to handle these edits.
When I run this script by browser works fine .. but when it runs using the helper job not running and I receive notification mail from cpanel with this error :
Please note I am on shared hosting plan with C-Panel so I have no root access .. but the code working fine when run from browser.
Error:
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
PHP Script:
exec('crontab -l', $crontab);
$record_found = false;
foreach($crontab as $key => $value){
if(strpos($value, 'record_extra.php') !== false){
//echo $key . ' ' . $value . '<br>';
unset($crontab[$key]);
$record_found = true;
}
if(strpos($value, 'record.php') !== false){
//echo $key . ' ' . $value . '<br>';
unset($crontab[$key]);
}
}
if($record_found){
file_put_contents('/tmp/crontab.txt', arrayToString($crontab) . $firstJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $secondJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
} else {
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $firstJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $secondJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
}
Need your help.
Thanks in advance.
Seems like the user isn't allowed to run crontab. Look into the files "/etc/cron.allow" and "/etc/cron.deny". If cron.allow exists, then only users in that list can use crontab. Users in cron.deny, can't use. If neither exist then only root user can use crontab.
I've looked at numerous "can't run crontab" posts and none of them addressed my problem. I want to report my unique solution.
My service provider copied my system to another place, and apparently they literally used the "cp" command to do it, which meant that no setuid bits were preserved. /usr/bin/crontab was one of several files for which I had to restore the setuid bit.
You can't run crontab if it doesn't have the setuid bit set.
I'm developing a new portal for my business.. I have to start many batch files for different pc names.. so I try to run my batch with one parameter..
$path = $db->givePath($service);
$path = $path . " " . "PCNAME";
if(exec("cmd /c" . $path)){
echo "Successful sent";
} else {
echo "Error";
}
But if I run this command, nothing happens..
The old version worked:
$path = $db->givePath($service);
if(exec("cmd /c" . $path)){
echo "Successful sent";
} else {
echo "Error";
}
Can someone assist me here?
You are likely encountering issues when passing the PCNAME as an argument because it has characters that need to be escaped. You can read more on escapeshellarg the TL;DR is it escapes any control characters that would cause the execution to exit in an unexpected way. Using something like UNC path for PCNAME withouth using escapshellarg() would trick windows into thinking there was another argument being specified.
$path = $db->givePath($service);
$batchCmd = "C:\{$path} " . escapeshellarg('PCNAME');
if(exec("cmd /c {$batchCmd}")) {
echo 'Successfully sent';
} else {
echo 'Error';
}
You can to use cmd shell:
system("cmd /c C:" . $path_to_file);
I'm trying to automate my testing. As a smoke test I would like to check my PHP code with PHPMD before continuing with the actual Unit tests. Sounds sensible enough right?
Thing is that PHPMD seems to crash when fatal errors arise in my PHP files. For a test I added an extra accolade at a function definition like so:
function foo() {{
// Stuff
}
Were I expected a 1 exit code, PHPMD seems to crash completely and instead returns a 0 exit code. Rendering my automated script useless. Is there a way to suppress these errors and return the expected exit code? For PHPUnit the --process-isolation option solved this problem but I can't seem to find such option for PHPMD.
Relevant automated testing code
#!/usr/bin/php
<?php
exec('meta/phpmd', $output, $returnCode);
if ($returnCode == 1) {
echo '[Fail] PHP code is breaking', PHP_EOL;
exit(1);
} elseif ($returnCode == 2) {
echo '[Warn] PHP code is unclean', PHP_EOL;
} else {
echo '[OK] Code is clean! ', PHP_EOL;
}
As a workaround (and possible solution) one could check the syntax before passing it to PHPMD. I changed my testing code to this:
#!/usr/bin/php
<?php
$dir_root = dirname(dirname(__DIR__));
$dir_php = $dir_root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'App';
exec('find ' . $dir_php . ' -iname *.php | xargs -n1 php -l 2>/dev/null', $output, $returnCode);
if ($returnCode != 0) {
echo '[Fail] PHP contains syntax errors', PHP_EOL,
implode(PHP_EOL, $output), PHP_EOL;
exit($returnCode);
}
exec('meta/phpmd', $output, $returnCode);
if ($returnCode == 1) {
echo '[Fail] PHP code is breaking', PHP_EOL;
exit(1);
} elseif ($returnCode == 2) {
echo '[Warn] PHP code is unclean', PHP_EOL;
}
Credits to Winglian at Reddit for the mass php -l code
https://www.reddit.com/r/PHP/comments/2t7mvc/lint_an_entire_directory_of_php_files_in_parallel/
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php exec command (or similar) to not wait for result
exec() waiting for a response in PHP
I have a php script that calls and runs a Matlab script. The result of the Matlab script is a .png image, which I would then like to load in php and send to a webpage. The php code I have is:
$matlabExe = '"C:\\Program Files\\MATLAB\\R2012a\\bin\\matlab.exe"';
$mFile = "'C:\\processSatData.m'";
$combine = '"run(' . $mFile . ');"';
$command = $matlabExe . ' -nodisplay -nosplash -nodesktop -r ' . $combine;
passthru($command);
$im = file_get_contents('C:\\habitat.png');
header('Content-type:image/png');
echo $im;
However, it appears that after sending the 'passthru' command, php does not wait for the Matlab script to finish running. Thus, if the image file does not exist before running the php code, then I get an error message.
Is there a way to make it so that the php code waits for the Matlab script to finish running before it attempts to load the image file?
passthru is not the main issue here .. but i guess as soon you have a response from your command the image is not written instantly but by a 3rd process
file_get_contents might also fail in this instance because .. The image might not be written once or in the process of writing which can result to file lock .. in any case you need to be sure you have a valid image before output is sent;
set_time_limit(0);
$timeout = 30; // sec
$output = 'C:\\habitat.png';
$matlabExe = '"C:\\Program Files\\MATLAB\\R2012a\\bin\\matlab.exe"';
$mFile = "'C:\\processSatData.m'";
$combine = '"run(' . $mFile . ');"';
$command = $matlabExe . ' -nodisplay -nosplash -nodesktop -r ' . $combine;
try {
if (! #unlink($output) && is_file($output))
throw new Exception("Unable to remove old file");
passthru($command);
$start = time();
while ( true ) {
// Check if file is readable
if (is_file($output) && is_readable($output)) {
$img = #imagecreatefrompng($output);
// Check if Math Lab is has finished writing to image
if ($img !== false) {
header('Content-type:image/png');
imagepng($img);
break;
}
}
// Check Timeout
if ((time() - $start) > $timeout) {
throw new Exception("Timeout Reached");
break;
}
}
} catch ( Exception $e ) {
echo $e->getMessage();
}
I believe if you change passthru to exec it will work as intended. You can also try this:
$matlabExe = '"C:\\Program Files\\MATLAB\\R2012a\\bin\\matlab.exe"';
$mFile = "'C:\\processSatData.m'";
$combine = '"run(' . $mFile . ');"';
$command = $matlabExe . ' -nodisplay -nosplash -nodesktop -r ' . $combine;
passthru($command);
// once a second, check for the file, up to 10 seconds
for ($i = 0; $i < 10; $i++) {
sleep(1);
if (false !== ($im = #file_get_contents('C:\\habitat.png'))) {
header('Content-type:image/png');
echo $im;
break;
}
}
I'm trying to get all .log and .txt files from an Ubuntu server using php 5.3.5 (WAMP). This is my third day using php ... total beginner. I'm reading some doc, but trying to accomplish useful tasks along the way, as to reinforce my learning. Additionally, when I use the code below, the .txt and .log files are printed in the browser but there is no structure (hard to read). How can I print each path on a single line (not sure if it should be apart of the sub process like echo -e \n in the ssh2exec function or a line I should add to the php code? Any help is appreciated ... thanks!
<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
if(!($ssh = ssh2_connect('10.5.32.12', 22))) {
echo "fail: unable to establish connection\n";
} else {
if(!ssh2_auth_password($ssh, 'root', '********')) {
echo "fail: unable to authenticate\n";
} else {
echo "Okay: Logged in ... \n";
$stream = ssh2_exec($ssh, 'find / -name *.log -o -name *.txt');
stream_set_blocking($stream, true);
$data = '';
while($buffer = fread($stream, 4096)) {
$data .= $buffer;
}
fclose($stream);
echo $data; // user
}
}
?>
In a shell, a new line is established by newline character ("\n"). In HTML, you'll need to either use CSS to make these newlines count:
echo '<div style="white-space: pre;">';
echo htmlspecialchars($data);
echo '</div>';
or insert <br/> elements:
echo nl2br(htmlspecialchars($data));
Here is a complete example, including download links and functionality:
<?php
if (! ($ssh = ssh2_connect('10.5.32.12', 22))) {
throw new Exception('Connection failed');
}
if (!ssh2_auth_password($ssh, 'root', '*******')) {
throw new Exception('Authentication failed');
}
if (isset($_GET['download'])) {
$fn = $_GET['download'];
if (! preg_match('/^[a-zA-Z0-9 .-_\\/]+(\\.txt|\\.log)$/', $fn)) {
throw new Exception('access denied');
}
header('X-Content-Type-Options: nosniff');
header('Content-Type: text/plain');
$sftp = ssh2_sftp($ssh);
$url = 'ssh2.sftp://' . $sftp . $fn;
readfile($url);
exit();
}
$stream = ssh2_exec($ssh, 'find / -name "*.log" -o -name "*.txt"');
stream_set_blocking($stream, true);
$data = stream_get_contents($stream);
$files = explode("\n", $data);
echo '<ul>';
foreach ($files as $f) {
if ($f == '') continue;
$url = $_SERVER['PHP_SELF'] . '?download=' . urlencode($f);
echo '<li><a href="' . htmlspecialchars($url) . '">';
echo htmlspecialchars($f);
echo '</a></li>';
}
echo '</ul>';