0

I'm new in PHP coding I'm writing new simple script but when i put this code i get blank page can some one tell me what's wrong with this code ?

<?php
if($_POST) {
$host = $_POST['host'];
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
if(!($con = ssh2_connect("127.0.0.1", "22")))
{
    echo "fail: unable to establish connection";
}
else
{
    if(!ssh2_auth_password($con, "root", "password"))
    {
        echo "fail: unable to authenticate ";
    }
    else
    {

        $stream = ssh2_exec($con, "".$host."");
            stream_set_blocking($stream, true);
        $item = "";
        while ($input = fread($stream,4096)) {
               $item .= $input;
        }
        echo $item;
    }
}

?>

sorry for my bad EN

codezero
  • 13
  • 1
  • 4
  • 1
    done any basic debugging, like enabling display_errors/error_reporting? – Marc B Feb 02 '15 at 20:35
  • please can you tell me how to do it ? i'm noob in PHP – codezero Feb 02 '15 at 20:37
  • http://php.net/manual/en/configuration.file.php – Marc B Feb 02 '15 at 20:37
  • white page of death, error reporting\display are off, turn them on `error_reporting(E_ALL); ini_set('display_errors', 1);` –  Feb 02 '15 at 20:39
  • It seems like you forgot a closing `}` character at the end of your script. You're not closing the `if($_POST) {`. This should also be `if(isset($_POST['host'])) {` I think. – Cyclonecode Feb 02 '15 at 20:40
  • i think the wrong in first 2 lines if($_POST) { $host = $_POST['host']; and the 16th line $stream = ssh2_exec($con, "".$host.""); am i right ? – codezero Feb 02 '15 at 20:40
  • 1
    @Cyclone yes you are right it's work now thank you all for helping me – codezero Feb 02 '15 at 20:42
  • @codezero - Like @MarcB said you should enable error reporting just add `error_reporting(E_ALL); ini_set('display_errors', true);` at the top of your script. – Cyclonecode Feb 02 '15 at 20:43
  • @cyclone: yes, but not via ini_set. they're best enabled at thephp.ini level, so you'll see fatal parse errors as well. there's no point in trying to enable them at the script level if the script doesn't even get to the point where it could execute the directives. – Marc B Feb 02 '15 at 20:44

3 Answers3

2

You might have better luck with phpseclib, a pure PHP SSH2 implementation. eg.

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>

If you want to let the command run for a certain amount of time before getting the output you can do $ssh->setTimeout(1). So you could do ping 127.0.0.1 on Linux, which won't stop, but still phpseclib would stop after one minute.

aqadiemond
  • 36
  • 1
0

I worked few days to make ssh2 work in PHP [dedicated server admin panel on www], but I did not find any solution for problem with output. Only thing that works (but this is good enough only for some scripts) is to sleep some time between 'exec' and 'read':

$stream = ssh2_exec($connection->conn, 'pgrep screen');
stream_set_blocking($stream, true);
// sleep 0.5 sec, this trick won't work for commands that execution time is unpredictable
usleep(500000);
$line = '';
while($get = fgets($stream))
{
    $line .= $get;
}
echo $line;
JerzySkalski
  • 624
  • 7
  • 9
0

Maybe its not the OP's question, but the title states php sshpass ssh, worth adding a simple php example using sshpass and ssh with exec().

$ssh_host = "127.0.0.1";
$ssh_port = "22";
$ssh_user = "root";
$ssh_pass = "password";
$command = "uname -a";
$connection = "/usr/bin/sshpass -p $ssh_pass /usr/bin/ssh -p $ssh_port -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $ssh_user@$ssh_host";
$output = exec($connection." ".$command." 2>&1");
echo "Output: $output";
Christoph Lösch
  • 645
  • 7
  • 22