[ Index ]

PHP Cross Reference of phpSSH

/ -> ssh_test.php (source)

   1  <?php
   2  /**
   3   * SExec test routines
   4   *
   5   * @package SExec
   6   * @author José R. Valverde <jrvalverde@acm.org>
   7   * @version 1.0
   8   * @copyright José R. Valverde <jrvalverde@es.embnet.org>
   9   *
  10   * This library is free software; you can redistribute it and/or
  11   * modify it under the terms of the GNU Lesser General Public
  12   * License as published by the Free Software Foundation; either
  13   * version 2.1 of the License, or (at your option) any later version.
  14   * 
  15   * This library is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18   * Lesser General Public License for more details.
  19   * 
  20   * You should have received a copy of the GNU Lesser General Public
  21   * License along with this library; if not, write to the Free Software
  22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23   *
  24   */
  25   
  26  // To test the class from the command line we need a shell with no control
  27  // terminal to avoid SSH from prompting interactively for the password:
  28  // run this with ' ssh -x -T localhost "(cd `pwd` ; php ssh_test.php)" '
  29  
  30  // To test on the web, just invoke this script.
  31  
  32  $debug_sexec = TRUE;
  33  
  34  require_once  'ssh.php';
  35  
  36  #$remote = "jruser@example.com";
  37  #$password = "PASSWORD";
  38  
  39  echo "<pre>\n";
  40  
  41  $rmt = new SExec($remote, $password);
  42  if ($rmt == FALSE) {
  43      echo "Couldn't open the connection\n";
  44      exit;
  45  }
  46  
  47  echo "Created\n";
  48  
  49  #$debug_passthru = TRUE;
  50  if (isset($debug_passthru)) {
  51      echo "Using passthru to send ls...\n\n";
  52      $rmt->ssh_passthru("ls", $status=0);
  53      echo "\nExit status was $status";
  54  }
  55  
  56  #$debug_exec = TRUE;
  57  if (isset($debug_exec)) {
  58      $rmt->ssh_exec("ls", $out="");
  59  #    $rmt->ssh_exec("ls");
  60      print_r($out);
  61      foreach ($out as $line)
  62          echo $line . "\n";
  63  }
  64  
  65  #$debug_copy_to = TRUE;
  66  if (isset($debug_copy_to)) {
  67      echo "\nCopy ldir ./CVS to rdir $remote:k/.\n";
  68      $rmt->ssh_copy_to("./CVS", "k", $out);
  69      echo "\nCopy lfile ./CVS/Entries to rdir $remote:k/.\n";
  70      $rmt->ssh_copy_to("./CVS/Entries", "k", $out);
  71      echo "\nCopy ldir ./CVS to rfile $remote:k/p\n";
  72      $rmt->ssh_copy_to("./CVS", "k/p", $out);
  73      echo "\nCopy lfile ./CVS/Entries to rfile $remote:k/p\n";
  74      $rmt->ssh_copy_to("./CVS/Entries", "k/p", $out);
  75  }
  76  
  77  #$debug_copy_from = TRUE;
  78  if (isset($debug_copy_from)) {
  79      echo "\nCopy rdir ./k/CVS to ldir k/.\n";
  80      $rmt->ssh_copy_from("./k/CVS", "k", $out);
  81      echo "\nCopy rfile ./k/CVS/Entries to ldir k/.\n";
  82      $rmt->ssh_copy_from("./k/CVS/Entries", "k", $out);
  83      echo "\nCopy rdir ./k/CVS to lfile k/p\n";
  84      $rmt->ssh_copy_from("./k/CVS", "k/p", $out);
  85      echo "\nCopy rfile ./k/CVS/Entries to lfile k/p\n";
  86      $rmt->ssh_copy_from("./k/CVS/Entries", "k/p", $out);
  87      $rmt->destruct() ; exit;
  88  }
  89  
  90  #$debug_persistent_shell = TRUE;
  91  if (isset($debug_persistent_shell))
  92  {
  93      $p = $rmt->ssh_open_shell();
  94      
  95      echo "after open I got this\n";
  96      print_r($p);
  97  
  98      sleep(1);
  99      echo "[1] flushing stdout after login\n";
 100      do {
 101      $line = fgets($p["std_out"], 1024);
 102      echo "> ".$line;
 103      } while ((! feof($p["std_out"]) ) && (! ereg("bossa-nova", $line)));
 104      $last = ftell($p["std_out"]);
 105  
 106      echo "\n[2]sending hello\n";
 107      fwrite($p['std_in'], "./hello\n"); fflush($p['std_in']);
 108      sleep(1); flush();
 109      fseek($p["std_out"], $last); 
 110      echo "< ";
 111      print_r(fgets($p["std_out"], 1024));
 112      echo "< ";
 113      print_r(fgets($p["std_out"], 1024));
 114      echo "\n";
 115      $last = ftell($p["std_out"]);
 116      fwrite($p['std_in'], "Oh yes I do\n"); fflush($p['std_in']);
 117      sleep(2);
 118  
 119      echo "flushing files\n\tstdout\n";
 120      flush();
 121      fseek($p["std_out"], $last);
 122      do {
 123      $line = fgets($p["std_out"], 1024);
 124      echo ">> ".$line;
 125      } while (! feof($p["std_out"]) );
 126      #    echo "stderr\n";
 127      #    do {
 128      #    $line = fgets($p["std_err"], 1024);
 129      #    echo ">>> ".$line."\n";
 130      #    } while (! feof($p['std_err']) && (strlen($line) != 0));;
 131  
 132      $rmt->ssh_close($p);
 133  }
 134  
 135  #$debug_persistent_cmd = TRUE;
 136  if (isset($debug_persistent_cmd))
 137  {
 138      $p = $rmt->ssh_open_command("ls");
 139      
 140      echo "after open I got this\n";
 141      print_r($p);
 142  
 143      sleep(1);
 144      echo "flushing files\nstdout\n";
 145      flush();
 146      rewind($p["std_out"]);
 147      do {
 148      $line = fgets($p["std_out"], 1024);
 149      echo "> ".$line;
 150      } while (! feof($p["std_out"]) );;
 151      #    echo "stderr\n";
 152      #    do {
 153      #    $line = fgets($p["std_err"], 1024);
 154      #    echo ">> ".$line."\n";
 155      #    } while (! feof($p['std_err']) && (strlen($line) != 0));;
 156  
 157      $rmt->ssh_close($p);
 158  }
 159  
 160  #$debug_popen = TRUE;
 161  if (isset($debug_popen)) {
 162      $cout = $rmt->ssh_popen("ls -C", "r");
 163      echo fread($cout, 8192);
 164      $rmt->ssh_pclose($cout);
 165      echo "\n";
 166      /*
 167      $cin = $rmt->ssh_popen("/usr/sbin/Mail -s test jr", "w");
 168         fputs($cin, "\nTest");
 169         fputs($cin, ".\n");
 170         fputs($cin, "\004");
 171      $rmt->ssh_pclose($cin);
 172      */
 173  }
 174  
 175  $rmt->destruct();
 176  
 177  
 178  echo "</pre>\n\n";
 179  exit;
 180  
 181  ?>


Generated: Tue May 31 15:44:47 2005 Cross-referenced by PHPXref 0.4.1