Tesseract exec not working - php

I have been trying to work this out for a couple days now and can't crack it.
I'm trying to use php to echo the result of tesseract.
After everything I've researched and tried, I feel like the below code should work.
<?php
echo '<pre>';
echo exec('/usr/local/bin/tesseract /home/username/www/ocr/images/hello.png result');
echo '</pre>';
?>
The command runs fine via SSH and if I change the above to suit ifconfig it works fine.
Any ideas to get this working?

You could try cat-ing the result as a 2nd command once tesseract is done. shell_exec appears to be better at returning the full output vs. exec.
<?php
$res = shell_exec('/opt/local/bin/tesseract /Users/stressederic/Sites/Sandbox/OCR/CC/gold.jpg result && cat result.txt');
var_dump($res);

I ended up getting this working by just breaking everything down.
file_put_contents("$tmpFile",file_get_contents($img));
$cmd = "/usr/local/bin/tesseract $tmpFile stdout";
exec($cmd, $msg);
$arraymsg = $msg;
$msg = implode(' ', $msg);
echo $msg;

its work
var_dump(exec('/usr/bin/tesseract 6.png out1 -l eng+ara'));
or
var_dump(shell_exec('/usr/bin/tesseract 6.png out1 -l eng+ara'));
tip:
in laravel =>
6.png Into the folder public
lang=> eng or ara are language

Related

how to get return value from python in php? it doesn't work

please help me... I have no idea.
This is part of my php code.
$result;
$resultD;
$arg= $ord." ".$time2_int;
$result=shell_exec('python3 decemberlist.py '.$arg);
$resultD=json_decode($result,true);
echo $resultD['song'];
And This is part of my python code.
import sys
import json
msg=''
def songName(a, b):
global msg
//
return msg
ret=songName(sys.argv[1], sys.argv[2])
rett={'song':ret}
print(rett)
print(json.dumps(rett, ensure_ascii=False))
php code print nothing. How can I solve this problem?
from here, shell_exec is disabled when PHP is running in safe mode.
but you can try this,
<?php
$result;
$resultD;
$arg= $ord." ".$time2_int;
$command = escapeshellcmd('python3 path/to/python_file/decemberlist.py '.$arg);
$result= shell_exec($command);
$resultD=json_decode($result,true);
echo $resultD['song'];
?>

Parsing SSH2 Results in PHP

I am running PHP vc9 NTS 5.3.28 on Windows Server 2003 Standard 32bit with phpseclib 0.3.6. I am trying to creating a script that will connect to a Palo Alto Networks firewall and execute a command to hash a password. I have the following code:
<?php
include 'Net/SSH2.php';
define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
$ssh = new Net_SSH2('hostname');
echo ">Logging in...\n";
if (!$ssh->login('user', 'password')) {
exit('Login Failed');
}
echo ">Reading login results...\n";
/*echo $ssh->exec('request password-hash password test123');*/
$output = $ssh->read('user#PA-3020>');
echo $output . "\n";
echo ">Writing request...\n";
$ssh->write("request password-hash password test123\n");
$ssh->setTimeout(10);
echo ">Reading result...\n";
$output = $ssh->read('/^\$1\$.*$/', NET_SSH2_READ_REGEX);
echo $output . "\n";
echo ">Done.\n";
file_put_contents ('E:\PHP53\ssh2.log', $ssh->getLog());
?>
I have two problems with the above code:
If I leave out the setTimeout(10) then the code never exists the next $ssh->read. If I have it in, then the code exists only after the timeout but does return results.
The results it returns are including a bunch of stuff that shouldn't be there:
?[Kuser#PA-3020> request password-hash password test123
?[?1h?=?[24;1H?[K
$1$dgkhwrxe$kddYFmKCq9.zfiBKPAyN61
?[24;1H?[K?[?1l?>user#PA-3020>
I only want the line that starts with $1$ (line 3 above). I figure it has something to do with the regex but I can't figure out what.
If I run the command interactively with PuTTY I get the following:
user#PA-3020> request password-hash password test123
$1$pxqhdlco$MRsVusWtItC3QiMm4W.xZ1
user#PA-3020>
UPDATE:
As per suggestions from neubert below, replacing the line with $output = $ssh->read... with the following code works:
$output = $ssh->read('/\$1\$.*/', NET_SSH2_READ_REGEX);
$output = preg_replace('/.*\$1\$/s','\$1\$', $output);
The results it returns are including a bunch of stuff that shouldn't
be there:
?[Kuser#PA-3020> request password-hash password test123
?[?1h?=?[24;1H?[K $1$dgkhwrxe$kddYFmKCq9.zfiBKPAyN61
?[24;1H?[K?[?1l?>user#PA-3020>
Those are ANSI escape codes. You can use File_ANSI to remove them. More info:
http://phpseclib.sourceforge.net/ssh/examples.html#top
Anyway, my guess would be that you need to redo your regex. eg.
$output = $ssh->read('/^\$1\$.*$/', NET_SSH2_READ_REGEX);
Instead of doing that do this:
$output = $ssh->read('/\$1\$/', NET_SSH2_READ_REGEX);
The thing is... ^ matches at the start of the line and $ matches at the end. Usually when you do $ssh->write(...) the command is echo'd back to you and then there's a new line and then you get your output back. So that'd prevent ^ from working. And as for the $ at the end.. well per your own example $1$ doesn't occur at the end of a line. So that's why your code isn't working.

How to provide default value for user input in PHP?

Is there a PHP functionallity same as read -i in BASH so that a script can prompt the user and provide a default answer like this:
Are you doing ok? (yes/no): yes
Where "yes" is the default answer provided by the script, which the user can erase and input another.
The readline function does not seem to have what it takes. Is there any other way to do this?
Using a stream does not seem to work either:
<?php
echo "Are you doing ok? (yes/no): ";
$in = fopen('php://stdin', 'rw+');
fputs($in, 'yes'); // should be the default?
$answer = fgets($in);
fclose($in);
echo "\nYou entered: {$answer}\n";
Whatever is in written by the fputs($in, 'yes'); line is ignored:
Are you doing ok? (yes/no): yes
You entered:
Am I using the stream incorrectly? Or maybe there is some other way to achive the default value?
EDIT:
Maybe I simlified the example to much. The real issue is not a simple yes/no prompt - this is just an example. Let me emphasize again: I'm aiming for providing exactly the same functionallity as the read -i BASH command. The $answer in my specific case holds an URL, so I would like for the user to be provided with the first part of the url (scheme, host, port), so he can add/edit the rest (path, query), fragment. Like this:
Enter url: http://www.example.com/foo/
now the user complement the path with bar/baz and we get:
You entered: http://www.example.com/foo/bar/baz
But on the other hand the user also should have the option to erase the first part of the url and provide completely different string:
Enter url: ftp://www.my-super-specific-domain.com/foo/bar
There's no built in way of doing this because writing some code that does it is quite straight forward:
function writeQuestion($question, $answers)
{
echo $question . ' (' . implode('/', $answers) . '): ' . PHP_EOL;
}
function readAnswer($possibleAnswers, $defaultAnswer)
{
$in = fopen('php://stdin', 'rw+');
$answer = trim(fgets($in));
if(!in_array($answer, $possibleAnswers))
{
return $defaultAnswer;
}
return $answer;
}
$question = 'Are you doing ok?';
$answers = array('yes', 'no');
$defaultAnswer = 'yes';
writeQuestion($question, $answers);
$answer = readAnswer($answers, $defaultAnswer);
You can wrap standard bash read for prefilled/populated edit and call it from you main php cli script
bash script named xreadline:
#! /bin/bash
IFS="";read -r -p "$1" -i "$2" -e STRING;echo "$STRING"
main php cli script:
#!/usr/bin/php
<?php
function xreadline ($prompt,$prefill)
{
$on = exec ('./xreadline "'.$prompt.'" "'.$prefill.'"');
return $on;
}
$answer = xreadline ('Your answer: ', 'xyz');
echo $answer;
.....
This approach fully supports UTF8 instead of native PHP readline.
Comment messed my code so here is standalone php cli file with function call to "read" command.
Little bit tricky, but hopefully will work as supposed to. :-)
#!/usr/bin/php
<?php
function xreadline ($prompt,$prefill)
{
return exec ('/bin/bash -c \'read -r -p "'.$prompt.'" -i "'.$prefill.'" -e STRING;echo "$STRING";\'');
}
echo "Prompt test :\n";
$output = xreadline ("edit this prefilled prompt: ","prefilled stuff");
echo "\n";
echo $output;

Php Exec function

I am unable to understand why the return value in php is not working. Can anybody help me?
<?php
exec("xyz.py",$output,$return);
foreach($output as $item){
echo "$item";
}
echo $return;
?>
The script of xyz.py is as follows:
def func():
print ('Hello')
return 21
func()
The output is always Hello0 no matter what value xyz.py returns
Thanks in advance.
According to the PHP docs, the third argument of exec, ($return in your example), works like this:
If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable
Since your python program ran fine, the return status should be 0 (no errors).
This might be what you want:
import sys
def func():
print ('Hello')
return 21
sys.exit(func())
Return parameter denotes the status of the exec() function and NOT what the program returns .
-1 = error
0 = success
Append this to your execution string, which allows you to capture any error output:
2>&1 &
So as per your example:
exec('xyz.py 2>&1 &', $output);
echo $output;

php exec() secure

I'm trying to execute a py file from php.
here is my code:
//usage python my.py var1 var2
$libre = 'python ../../../../root/py/my.py '.$var1.' '.$var2.'';
$cleanlibre = escapeshellarg($libre);
echo exec($cleanlibre);
What is wrong?
Why is it returning nothing?
I also need to know how to secure exec well. Thanks.
--edit--
Used passthru
$libre = 'python ../../../../root/py/mech.py '.$var1.' '.$var2.'';
$cleanlibre = escapeshellarg($libre);
passthru($cleanlibre, $result);
echo $result;
//returned 127 <- i don't know where thats from.
escapeshellarg shall only be used to escape the arguments, not the whole command.
//usage python my.py var1 var2
$libre = 'python ../../../../root/py/my.py '.escapeshellarg($var1).' '.escapeshellarg($var2).'';
echo exec($libre );
exec return result in second argument of function, see http://php.net/manual/en/function.exec.php

Categories