PHP Output correct in terminal but not in browser - ssh

I just started working with shell_exec in php and stuck at this point.
Below is my php script which runs in terminal correctly but not in browser.
<?php
echo shell_exec("ssh -tq root#192.168.31.5 \"whoami\"");
?>
And output in terminal is
$ php /var/www/html/monitor/ssh.php
root
But in Browser,
Interesting thing is just whoami works like a charm
<?php
echo shell_exec("whoami");
?>
any suggetion is appriciated. Thank you!
EDIT :- USING OB_START() and OB_GET_CONTENT
<?php
ob_start();
echo shell_exec("ssh -tq root#192.168.31.5 \"whoami\"");
$out1 = ob_get_contents();
ob_end_clean();
var_dump($out1);
?>
OUTPUT IN TERMINAL :-
php /var/www/html/monitor/ssh.php
string(6) "root"
OUTPUT IN BROWSER (CHROME) :-
string(0) ""

That's because in CLI you're executing the script as the user from SSH (root in your case) but in browser, the one executing the script is your WebServer (apache/nginx). For you to get root as output in browser you might want to have a look at ob_start ob_get_contents ob_flush functions.

Related

How to create htaccess file correctly?

I am trying to run a script via apache on a shared linux server, like
#!/usr/bin/perl
print "Content-type: text/html\n\n";
foreach $i (keys %ENV) {
print "$i $ENV{$i}\n";
}
But I want to run it via. a symlink created like this
ln -s printenv.pl linkedprintenv.pl
It runs fine directly but I got a 500 server error when executing via the symlink from a web browser. I understand the solution may be to create a .htaccess file containing
Options +FollowSymLinks
but I tried and that didn't work. Is there extra configuration needed for that single line htaccess file to take effect?

How to get data from commandline wappalyzer phantomjs?

I need to get data of url using wappalyzer phantomjs.When i am running phantomjs on commandline then it is working fine.but i need to get the json response on webpage.Can i get the response on webpage using php or some other way.I am not getting full example of it.Please help.
Yes you can do it with php.
$output = shell_exec('/usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs --ssl-protocol=any --ignore-ssl-errors=true driver.js http://example.com');
echo json_decode($output,true);
Note : Change this "/usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/" to directory where you installed the phantomjs.
You need to execute the command:
$output = shell_exec('/usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs --ssl-protocol=any --ignore-ssl-errors=true driver.js http://example.com'); echo json_decode($output,true);
Note : Change this "/usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/" to directory where you installed the phantomjs.

Loading of PNG image through CGI program failing with errors

I am writing to write a cgi script to display a png image in a browser. When user clicks on the 'Submit' form in the HTML page , the CGI program is called to display the image .
But this is not working for me . The script is trying to open the PNG image in a browser,but the image contains the following error message
"The image “http://localhost/cgi-bin/image.sh?subbtn=Submit” cannot be displayed, because it contains errors."
The following is the snippet of CGI code.
!/bin/bash
echo "Content-type: text/html"
echo "Content-type: image/png"
echo ""
echo "<html>"
echo "<body>"
echo "Hi"
echo "<img src="/home/zaman/ssdggraph/SSDGhistory.png" alt="DG-Reports">"
echo "</body>"
echo "</html>"
If I remove the below line from the above code , then the text "DG-Reports" in alt section from img src tag is displayed.
echo "Content-type: image/png"
Also if I write the same code in plain HTML page , the PNG image is displayed fine.
Please suggest what I am missing in the code to display the image without any errors ?
You should specify a URL for the <IMG>'s src attribute that can be reached from the browser.
/home/zaman/ssdggraph/SSDGhistory.png is unlikely to to satisfy that requirement. (You should probably have someting like http://path_known_to_webserver/SSDGhistory.png).
You should probably also read up on the concepts of web pages and cgi scripts. I would call a CGI "that displays an image", some program that would be referenced in the above URL and dynamically streams out the image data once the web pages refers to it and a browser asks for it. That might be what you mean, but it is kind of unclear.
You have to use content type, then blank line:
#!/bin/bash
echo "Content-type: image/png"
echo
cat /var/www/img/your-image.png

Cron job don't execute specific php script

I setup a cron job (in Virtualmin based of Webmin) for execute a simple test script and it work well.
The cron command used is:
/usr/bin/php -q /home/myuser/domains/mysite.com/public_html/mailtest.php
The url is:
www.mysite.com/mailtest.php
The content of "mailtest.php" is:
<?php
$to = "your#mail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "any#any.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Now I tried to execute another script and I have of course modify path and file name who is simply:
/usr/bin/php -q /home/myuser/domains/mysite.com/public_html/myfolder/myscript.php
But here I don't know why my script is not executed. When I go to his related url
"www.mysite.com/myfolder/myscript.php" the script is well executed.
The content of "myscript.php" is:
<?php
require_once(dirname(__FILE__).'/includes/includes.php');
$mails = POP3Mailer::ProcessMails();
require_once($BASE_PATH.'/includes/footer.php');
?>
Somebody have an idea why the cron won't fire with "myscript.php" ???
Thank for your time
I'm not really sure about it. But maybe you should check permissions on this file.
Use the chmod command.
Cron is working in very basic environment. Create new file. Make it executable (chmod +x) and add it to cron. Add #!/bin/bash at the begining of the file and than your script via /usr/local/bin/php.
Your file should look like :
#!/bin/bash
/usr/local/bin/php /url/to/your/script.php

phpinfo() outputs nothing

I've tried phpinfo() but it output nothing. It is interesting that php-r "phpinfo();" works correctly while using phpinfo() in web outputs nothing.
Again, nothing is written to error.log too. php.ini is empty.
I don't know what to do now.
Edit:
Thanks to lanzz, I got that phpinfo() requires no output before it.
I had a similar issue. I am running Apache2 on my Ubuntu machine for local testing of projects before deployment. I created a phpinfo(); page, and noticed it was blank. Upon inspecting in my browser, the php function would be commented out.
Your browser has nothing to do with php.
In order for PHP scripts to execute, you have to save the file as a .php file, which I'm sure you did.
PHP tags alone aren't valid in HTML documents, so you need to create an actual HTML document, and save it as .php. Then, include a basic HTML document like so:
<?php
phpinfo();
?>
<html>
<head>
<title>Php Info</title>
</head>
<body>
</body>
</html>
Then you would do:
localhost/phpinfo.php or whatever you're working on. In my case it was an actual domain name that was mapped to my localhost, so 'myProjectName'.com/phpinfo.php
This is just from my experience and it worked 100%.
Just wanted to add, that I was using short tags '<?'
<? phpinfo();
which was failing, becuase I was using short tags when they were disabled so it should have been
<?php phpinfo();
First, Check the content of your "access.log" ; if you don't see the call to the phpinfo file, you certainly have a problem on your web server.
Otherwise, try to get a simple php file :
<?php echo "Hello, world.";
Or HTML file :
<h1>Hello, world !</h1>
Then you can determine where is the problem.
Perhaps the PHP module for the web server isn't loaded.
One more case when there is no output from php file is when permission is set incorrectly. For example your web server has no rights to read it.
I had to use a workaround to make it work. The code I used was: I can't answer why it only works like this but it does.
<?php
echo phpinfo();
?>
In my case (Ubuntu 22.04), apache was not properly configured to run php / apache's mpm_prefork module was conflicting with php. I followed the instructions in this answer to solve the problem. Instructions reposted here for convenience.
sudo a2dismod mpm_event
sudo systemctl restart apache2
sudo a2enmod mpm_prefork
sudo systemctl restart apache2
sudo a2enmod php7.0
sudo systemctl restart apache2