PHP & SSH2 getting console output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mix2
    New Member
    • Feb 2010
    • 2

    PHP & SSH2 getting console output

    Hi, i use php to run certain commands at my remote linux server,
    everything works fine, but i cant figure out how to get output of console after executing commands.
    I tryed stream_get_cont ents but it does not work :(

    Anyone have any ideas?

    Code:
    <?php 
    if(isset($_POST['submitinupp'])) 
    { 
    $connection = ssh2_connect('server.example.com', 22); 
    ssh2_auth_password($connection, 'root', 'topsecret'); 
    $name = $_POST["user"]; 
    $pass = $_POST["pass"]; 
    $stream = ssh2_exec($connection, "./test.sh $name $pass"); 
    echo stream_get_contents($stream); 
    } 
    ?>
    Thanks in advantage
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    You can use the interactive shell. or just use exec() or system() calls to do your ssh authentication.

    Well actually ^ that's just an idea. Never tried it.




    Dan

    Comment

    • Mix2
      New Member
      • Feb 2010
      • 2

      #3
      Sorry i am not a professional php programmer, cant really understand hos exec() or system() would help me...

      Maybe i explained my wish little badly.


      Basicly for example if i send a command "whoami" to server, how could i echo answer what comes from ssh in php ?

      For example:
      Code:
      <?php 
      if(isset($_POST['submitinupp'])) 
      { 
      $connection = ssh2_connect('server.example.com', 22); 
      ssh2_auth_password($connection, 'root', 'topsecret'); 
      ssh2_exec($connection, "whoami"); 
      echo ??????
      } 
      ?>

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        ssh2_exec() returns a stream right? just read that stream.

        Great examples found on this blog via Google: http://kevin.vanzonneveld.net/techbl...ions_with_php/



        Dan

        Comment

        Working...