How to print the output of a shell command in php? - php

I want to print the output of a command named jnettop into an html file.
I am very familiar with shell_exec() and sort.
But it doesnt work as the command that Im using doesnt have a bash mode like top that is described here.
The end goal is have a site that I go to and I see the information that jnettop would normally display in terminal.
Here is the link to jnettops wiki page.

You might try passthru() or exec() with the output argument peresent.

the command that Im using doesnt have a bash mode like top
Sure it has: jnettop --display text.

Related

PHP exec and shell_exec: no output

Yesterday I asked this question
Building g++ via PHP: no build output
That part is solved, but now I now have a similar/related problem.
When I run the compiled executable (see other question) via my browser address bar, the output is printed to the browser window as expected.
However ,I want to use it within PHP and get the output via the return of the exec or shell_exec functions. I have tried various things, including the following:
$output = "test.cgi";
echo shell_exec($output);
echo shell_exec("$output 2>&1");
echo system($output);
Nothing works..
Internally, the executable is c code, compiled by g++, and is using print_f. That should output to STDOUT by default accoding to the specs, so I do not understand why this isn't working.
Can anyone help?
For anyone with the same problem, I found the solution:
exec needed an absolute path to the file

Using PHP to call Virtualenv’ed Python Script

Last night I spent 5.5 hours trying make PHP execute and receive the output of Virtualenv’ed Python script. Nothing worked; except for scripts that were not Virtualenv’ed.
What I am trying to do:
I am trying to make PHP call a virtualenv’d install of the Newspaper lib output text when I call it.
What I have now:
PHP: (updated)
<?php
$output = exec('newspaper2/bin/python3 /var/www/html/components/python/test.py 2>&1', $output2);
print_r(error_get_last());
echo $output2;
echo $output;
…this works when using a non-virtualenv script
Python: (updated)
from newspaper import Article
url = 'http://example.com/'
article = Article(url)
article.download()
article.html
article.parse()
article.authors
article.publish_date
string = article.text
print(string)
What the issue is:
I can run the script that PHP is running from the command line and it outputs just fine.
What I have tried:
With PHP, (I have tried all the “exec” calls for PHP) it cannot seem to open the virtual environment and returns nothing.
Before the script I have called “python3” and a few other things to no avail.
Yes, I have chmoded it to be executable…
I feel like this should be so simple.
I have tried suggestions on other posts and all over the web to no avail.
Questions:
Did I set up the virtualenv wrong?
At the top of the Python script, instead of the “#!/usr/bin/env python3” should I call something else?
If so, where do I find it? Should I start from scratch and will that
help?
Thank you for your help;
PS: I am running Ubuntu16, PHP7 and I need to use Python3
In the virtualenv'ed scripts (i.e. installed via the setuptools' entry-points), you should not touch the shebang (#!... first line). It is populated by the virtualenv & setuptools & related tools.
If you specify your own shebang, then it is not virtualenv'ed script. In that case, call python directly:
exec('/path/to/venv/bin/python3 /var/www/html/components/python/testing.py');
Alternatively, you can put the absolute path to the virtualenv's python binary to the py-script, but this does not look a good idea.
Also, remember that virtualenvs are non-relocatable. So they should stay in the path where they were created.
Also note that exec() returns only the last line of the output. You probably want shell_exec() or exec('...', $output) to get the whole output.
Also, it is unclear what happens with your script, and what is being printed on stderr. Try this command to see what is the error:
exec('/path/to/script 2>&1', $output)
#OR:
exec('/path/to/venv/bin/python3 /path/to/script 2>&1', $output)
OK, I finally figured it out and learned a lot in the process. The newspaper lib that I am using by default tries to write to the base of the users home directory. In this case, it was attempting to write to www-data, /var/www.
To fix this:
Go to the settings.py file in the newspaper library.
Edit the variable DATA_DIRECTORY = '.newspaper_scraper' and change it to DATA_DIRECTORY = '.path/to/writable/directory'
Save the file and you should be good to go.
I have no idea why it was not returning the errors that would have explained this sooner.
Hope this helps anyone else.
Thank you so much Sergey Vasilyev for your help. I appreciate it greatly.

confused about streams

I am running a command in windows like :
command.exe > file.txt
and I get a text in the file, but when I run command.exe directly it does not shows that text on the console. The command should have outputted to stdout, but it didn't. Is there a way I can find out which stream the text goes to and read that text using php.
Thanks in advance
Running shell commands in PHP is like calling shell_exec() or any of the linked functions there. The output of the command goes to where the doc says.
For example passthru() sends the output directly to the browser.

Print on php page what runs on background (Terminal) php_exec

I run a soft like ffmpeg with exec function. What I want is to see what is happening on the terminal the same time the command runs, I don't know if I can do it with echo or not. So, I need your help.
Thank you !
If you want to show the output of a command before it has finished, you should use a combination of popen() and flush(). There is a good example in the php.net docs, see http://www.php.net/manual/en/function.popen.php#56604

PHP exec command has no effect despite returning no error code

I'm running a command to add id3 metadata to mp3 files via PHP's exec function, like so:
exec('id3v2 [options, filename etc go here] 2>&1', $output, $result);
The command is currently having no effect on the target files (ie. their id3 tags are not being updated). However, it returns 0 as the $result, outputs no errors, and if I run the exact same command from the command line it works as expected.
If anyone could suggest what I'm doing wrong, I'd be very grateful!
Check the $output.
This is probably a path or access right issue. For example if you run this script with the web-server priviliges it probably doesn't have the right to alter the files.
Does the user PHP is running under have permissions to write to those files and execute "id3v2"?
Try http://www.php.net/manual/en/language.operators.execution.php
Or, if id3v2 returns something you can do http://php.net/manual/en/function.shell-exec.php

Categories