LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   simple php ssh script does not display on browser (http://www.linuxquestions.org/questions/programming-9/simple-php-ssh-script-does-not-display-on-browser-4175426494/)

figure20012 09-10-2012 07:17 AM

simple php ssh script does not display on browser
 
<html>
<body>
<?php
$var=`ssh 127.0.0.1 ls /`;
print "$var";

?>
<body>
</html>
ssh promts me to enter my password
when i run script.php on bash it outputs but when i run it on my browser it does not output anything
please help how i can show the results on my browser

michaelk 09-10-2012 12:47 PM

What are you trying to accomplish?

With the information provided I would say that when running from a console your script is being interpreted by bash (or other shell) and not php. Since your script is not proper php syntax it fails when run from the browser.

Here is how to run an external command from php.
http://php.net/manual/en/function.exec.php

Here is how to run a command via ssh.
http://php.net/manual/en/function.ssh2-exec.php

FYI when posting code please use code tags to preserve formatting.

theNbomr 09-12-2012 09:33 AM

The Web server, and thus your PHP code, runs as a different UID than your own UID. The UID of the web server requires a password to login, if it's UID allows logins at all. Perhaps specify the username for whom you want the directory listing to be done.
Code:

ssh joeBloggs@127.0.0.1 ls /
--- rod.

YankeePride13 09-12-2012 10:28 AM

PHP is a server side programming language. If you need to enter a password for SSH to work in the browser, then it is already too late because the PHP code has been executed. You'd need to put the password in the code somewhere (preferably in an include with read permissions to only apache or whatever web server you use and root) in order to log in.

In other words, once the server passed the page to the browser, PHP has been completely run. The browser just gets html/xml/javascript that PHP generates.


All times are GMT -5. The time now is 02:21 AM.