3

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, Browser Output

Interesting thing is just whoami works like a charm

<?php
    echo shell_exec("whoami");
?>

enter image description here

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) ""

1 Answers1

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.

darklightcode
  • 2,738
  • 1
  • 14
  • 17
  • but i am logging into other computer through ssh as a root user. The problem is when i execute script on the current computer/server it works fine but same command with ssh on other computer/server doesn't works. – Dirty_Programmer Mar 23 '19 at 13:13