I have this bash file which generates key and expire time :
#!/bin/bash
get_customer_url() {
local IP=${1:-127.0.0.1}
local SECRET=${2:-VERY_COOL_SECRET}
local EXPIRES="$(date -d "today + 30 minutes" +%s)";
local token="$(echo -n "${EXPIRES} VERY_COOL_SECRET" | openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =)"
echo "/${token}/${EXPIRES}/"
}
get_customer_url
and i call it via php like this
<?php $output = shell_exec('sh generate-keys-hls.sh'); echo "$output";?>
it works fine , it generates something like this
/G8INKLc3VfDMYtQT2NTU-w/1530222035/
my problem is i want to put another php result in the some line like this
<?php $output = shell_exec('sh generate-keys-hls.sh'); echo "$output";?><?php echo get_post_meta($post->ID, 'file', true)?>
currently its printing the result in two lines i need it to be one just one line instead of this
/G8INKLc3VfDMYtQT2NTU-w/1530222035/
file.mp4.m3u8
i want it to be like this
/G8INKLc3VfDMYtQT2NTU-w/1530222035/file.mp4.m3u8
without white spaces or multiple lines !
Remove the whitespace around the result of shell_exec().
echo trim($output);
Related
New to kubernetes and php, so I'm having some issues. Any and all help is greatly appreciated!
<?php
$postgres = 'kubectl get pods -n migrationnamespace | grep postgres | cut -d " " -f1 2>&1';
$postgres_pod = shell_exec($postgres);
echo $postgres_pod;
$list2 = 'kubectl exec -it -n migrationnamespace ' . $postgres_pod . ' -- psql -U postgres -c \'SELECT * FROM mywhales\'; 2>&1';
echo "<pre>";
echo shell_exec($list2);
echo "<pre>";
?>
results in error
postgres-7957478b7d-tmw6m
error: you must specify at least one command for the container
sh: line 1: --: command not found
When switching '.$postgres_pod.' for postgres-7957478b7d-tmw6m as below - it executes fully
$list2 = 'kubectl exec -it -n migrationnamespace postgres-7957478b7d-tmw6m -- psql -U postgres -c \'SELECT * FROM mywhales\';';
postgres-7957478b7d-tmw6m
whale
---------
16:117
......
561:539
(17 rows)
Thanks - Mike
There can sometimes be extra whitespace before or after strings, especially return characters which don't always show up when echoing the result.
Using trim($postgres_pod) will ensure they are removed.
So i have two codes, one of them is just a simple code that i used to test out some php functions. I'm trying to print out the JSON string into my html, one of the codes is working but the other isn't.
So let's say that we have two sets of codes : first.php & first.py , second.php & second.py
first.php looks like this:
<?php
exec("C:/Users/hln/Anaconda3/envs/tensorflow1/python.exe C:/tensorflow1/models/research/object_detection/first.py", $output);
$someOutput = json_decode($output[0], true);
echo "<h3>" . $someOutput['rightCoordinate'] ."</h3>";
echo "<h3>" . $someOutput['leftCoordinate'] ."</h3>";?>
first.py looks like:
import json
a = 1 + 3
b = 5 + 5
x = {
"leftCoordinate": a,
"rightCoordinate": b
}
y = json.dumps(x)
print(y)
print()
second.php looks like this:
<?php
exec("C:/Users/hln/Anaconda3/envs/tensorflow1/python.exe C:/tensorflow1/models/research/object_detection/second.py C:/xampp/htdocs/w3layout/finalproject/uploads/10.PNG 10.PNG 2>&1",$output);
$someOutput = json_decode($output[0], true);
echo "<h3>" . $someOutput['theWidth'] ."</h3>"; ?>
second.py looks like:
outputvalues = {
"leftCoordinate" : x_min(each of these are already defined),
"rightCoordinate" : x_max,
"lowerCoordinate" : y_min,
"upperCoordinate" : y_max,
"numInjuries" : count,
"theWidth" : im_width,
"theHeight" : im_height
}
y = json.dumps(outputvalues)
print(y)
when i run them in command prompt, the first one will result:
{"leftCoordinate": 4, "rightCoordinate": 10}
and when i put it in my html it will print out 4 and 10
the second one have this result in command prompt:
{"leftCoordinate": 34.47790487855673, "rightCoordinate": 251.67991018295288, "lowerCoordinate": 208.6769086420536, "upperCoordinate": 388.4499931335449, "numInjuries": 1, "theWidth": 327, "theHeight": 503}
but it won't print out any result in html
is there anything that i should change?
This might not be a helpful answer because there does not seem to be anything wrong with your code.
I placed the following at the top of second.py:
import json
x_min = 34.47790487855673
x_max = 251.67991018295288
y_min = 208.6769086420536
y_max = 388.4499931335449
count = 1
im_width = 327
im_height = 503
and removed the absolute paths in second.php:
exec("second.py 10.PNG 10.PNG 2>&1",$output);
Now php second.php shows me <h3>327</h3>.
This is on Windows 10 with PHP 7.3.1 and Python 3.7.4.
Edit: Tried it on Ubuntu 19.04/PHP 7.2.19/Python 2.7.16 and got the same result.
Based on your comments the problem is the fact that the Python script generates a warning which is then passed to the PHP script.
One way to ignore the warnings is to add the -W option with a value ignore. The line in the PHP script would then look something like this:
exec("python -W ignore second.py", $output);
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
Is it possible to pass BASH associative arrays as argv to PHP scripts?
I have a bash script, that collects some variables to a bash associative array like this. After that, I need to send it to PHP script:
typeset -A DATA
DATA[foo]=$(some_bash_function "param1" "param2")
DATA[bar]=$(some_other_bash_function)
php script.php --data ${DATA[#]}
From PHP script, i need to access the array in following manner:
<?php
$vars = getopt("",array(
"data:"
));
$data = $vars['data'];
foreach ($data as $k=>$v) {
echo "$k is $v";
}
?>
What I've tried
Weird syntax around the --data parameter follows advice from a great post about bash arrays from Norbert Kéri how to force passed parameter as an array:
You have no way of signaling to the function that you are passing an array. You get N positional parameters, with no information about the datatypes of each.
However this sollution still does not work for associative arrays - only values are passed to the function. Norbert Kéri made a follow up article about that, however its eval based solution does not work for me, as I need to pass the actual array as a parameter.
Is the thing I'm trying to achieve impossible or is there some way? Thank you!
Update: What I am trying to accomplish
I have a few PHP configuration files of following structure:
<?php
return array(
'option1' => 'foo',
'option2' => 'bar'
)
My bash script collects data from user input (through bash read function) and stores them into bash associative array. This array should be later passed as an argument to PHP script.
php script.php --file "config/config.php" --data $BASH_ASSOC_ARRAY
So instead of complicated seds functions etc. I can do simple:
<?php
$bash_input = getopt('',array('file:,data:'));
$data = $bash_input['data'];
$config = require($config_file);
$config['option1'] = $data['option1'];
$config['option2'] = $data['option2'];
// or
foreach ($data as $k=>$v) {
$config[$k] = $v;
}
// print to config file
file_put_contents($file, "<?php \n \n return ".var_export($config,true).";");
?>
This is used for configuring Laravel config files
Different Approach to #will's
Your bash script:
typeset -A DATA
foo=$(some_bash_function "param1" "param2")
bar=$(some_other_bash_function)
php script.php "{'data': '$foo', 'data2': '$bar'}"
PHP Script
<?php
$vars = json_decode($argv[1]);
$data = $vars['data'];
foreach ($data as $k=>$v) {
echo "$k is $v";
}
?>
EDIT (better approach) Credit to #will
typeset -A DATA
DATA[foo]=$(some_bash_function "param1" "param2")
DATA[bar]=$(some_other_bash_function)
php script.php echo -n "{"; for key in ${!DATA[#]}; do echo - "'$key'":"'${DATA[$key]}'", | sed 's/ /,/g' ; done; echo -n "}"
this does what you want (i think) all in one bash script. You can obviously move the php file out though.
declare -A assoc_array=([key1]=value1 [key2]=value2 [key3]=value3 [key4]=value4)
#These don't come out necesarily ordered
echo ${assoc_array[#]} #echos values
echo ${!assoc_array[#]} #echos keys
echo "" > tmp
for key in ${!assoc_array[#]}
do
echo $key:${assoc_array[$key]} >> tmp # Use some delimeter here to split the keys from the values
done
cat > file.php << EOF
<?php
\$fileArray = explode("\n", file_get_contents("tmp"));
\$data = array();
foreach(\$fileArray as \$line){
\$entry = explode(":", \$line);
\$data[\$entry[0]] = \$entry[1];
}
var_dump(\$data);
?>
EOF
php file.php
the escaping is necessary in the cat block annoyingly.
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.