Main Page | Namespace List | File List | File Members

ssh_test.php

Go to the documentation of this file.
00001 <?php
00002 /**
00003  * SExec test routines
00004  *
00005  * @package SExec
00006  * @author José R. Valverde <jrvalverde@acm.org>
00007  * @version 1.0
00008  * @copyright José R. Valverde <jrvalverde@es.embnet.org>
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  * 
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  */
00025  
00026 // To test the class from the command line we need a shell with no control
00027 // terminal to avoid SSH from prompting interactively for the password:
00028 // run this with ' ssh -x -T localhost "(cd `pwd` ; php ssh_test.php)" '
00029 
00030 // To test on the web, just invoke this script.
00031 
00032 $debug_sexec = TRUE;
00033 
00034 require_once 'ssh.php';
00035 
00036 //$remote = "jruser@example.com";
00037 //$password = "XXX";
00038 
00039 echo "<pre>\n";
00040 
00041 $rmt = new SExec($remote, $password);
00042 if ($rmt == FALSE) {
00043     echo "Couldn't open the connection\n";
00044     exit;
00045 }
00046 
00047 echo "Created\n";
00048 
00049 #$debug_passthru = TRUE;
00050 if (isset($debug_passthru)) {
00051     echo "Using passthru to send ls...\n\n";
00052     $rmt->ssh_passthru("ls", $status=0);
00053     echo "\nExit status was $status";
00054 }
00055 
00056 #$debug_exec = TRUE;
00057 if (isset($debug_exec)) {
00058     $rmt->ssh_exec("ls", $out="");
00059 #    $rmt->ssh_exec("ls");
00060     print_r($out);
00061     foreach ($out as $line)
00062         echo $line . "\n";
00063 }
00064 
00065 #$debug_copy_to = TRUE;
00066 if (isset($debug_copy_to)) {
00067     echo "\nCopy ldir ./CVS to rdir $remote:k/.\n";
00068     $rmt->ssh_copy_to("./CVS", "k", $out);
00069     echo "\nCopy lfile ./CVS/Entries to rdir $remote:k/.\n";
00070     $rmt->ssh_copy_to("./CVS/Entries", "k", $out);
00071     echo "\nCopy ldir ./CVS to rfile $remote:k/p\n";
00072     $rmt->ssh_copy_to("./CVS", "k/p", $out);
00073     echo "\nCopy lfile ./CVS/Entries to rfile $remote:k/p\n";
00074     $rmt->ssh_copy_to("./CVS/Entries", "k/p", $out);
00075 }
00076 
00077 #$debug_copy_from = TRUE;
00078 if (isset($debug_copy_from)) {
00079     echo "\nCopy rdir ./k/CVS to ldir k/.\n";
00080     $rmt->ssh_copy_from("./k/CVS", "k", $out);
00081     echo "\nCopy rfile ./k/CVS/Entries to ldir k/.\n";
00082     $rmt->ssh_copy_from("./k/CVS/Entries", "k", $out);
00083     echo "\nCopy rdir ./k/CVS to lfile k/p\n";
00084     $rmt->ssh_copy_from("./k/CVS", "k/p", $out);
00085     echo "\nCopy rfile ./k/CVS/Entries to lfile k/p\n";
00086     $rmt->ssh_copy_from("./k/CVS/Entries", "k/p", $out);
00087     $rmt->destruct() ; exit;
00088 }
00089 
00090 #$debug_persistent_shell = TRUE;
00091 if (isset($debug_persistent_shell))
00092 {
00093     $p = $rmt->ssh_open_shell();
00094     
00095     echo "after open I got this\n";
00096     print_r($p);
00097 
00098     sleep(1);
00099     echo "[1] flushing stdout after login\n";
00100     do {
00101         $line = fgets($p["std_out"], 1024);
00102         echo "> ".$line;
00103     } while ((! feof($p["std_out"]) ) && (! ereg("bossa-nova", $line)));
00104     $last = ftell($p["std_out"]);
00105 
00106     echo "\n[2]sending hello\n";
00107     fwrite($p['std_in'], "./hello\n"); fflush($p['std_in']);
00108     sleep(1); flush();
00109     fseek($p["std_out"], $last); 
00110     echo "< ";
00111     print_r(fgets($p["std_out"], 1024));
00112     echo "< ";
00113     print_r(fgets($p["std_out"], 1024));
00114     echo "\n";
00115     $last = ftell($p["std_out"]);
00116     fwrite($p['std_in'], "Oh yes I do\n"); fflush($p['std_in']);
00117     sleep(2);
00118 
00119     echo "flushing files\n\tstdout\n";
00120     flush();
00121     fseek($p["std_out"], $last);
00122     do {
00123         $line = fgets($p["std_out"], 1024);
00124         echo ">> ".$line;
00125     } while (! feof($p["std_out"]) );
00126     #    echo "stderr\n";
00127     #    do {
00128     #   $line = fgets($p["std_err"], 1024);
00129     #   echo ">>> ".$line."\n";
00130     #    } while (! feof($p['std_err']) && (strlen($line) != 0));;
00131 
00132     $rmt->ssh_close($p);
00133 }
00134 
00135 $debug_persistent_cmd = TRUE;
00136 if (isset($debug_persistent_cmd))
00137 {
00138     echo "\nCopying interactive script to $remote:\n";
00139     $rmt->ssh_copy_to("./interact.sh", ".", $out);
00140     
00141     $p = $rmt->ssh_open_command("./interact.sh");
00142     echo "after open I got this\n";
00143     print_r($p);
00144     sleep(1);
00145     
00146     $line = fgets($p["std_out"], 1024);
00147     echo "> ".$line ."\n";
00148     $line = fgets($p["std_out"], 1024);
00149     echo "> ".$line ."\n";
00150     sleep(1);
00151     
00152     echo "\nSending 'SExecClient'\n";
00153     fwrite($p['std_in'], 'SExecClient\n'); fflush($p['std_in']);
00154     sleep(1);
00155     $out = fgets($p['std_out'], 1024);
00156     echo "\nGot [". $out . "]\n";
00157 
00158     echo "flushing files\nstdout\n";
00159     flush();
00160     do {
00161         $line = fgets($p["std_out"], 1024);
00162         echo "> ".$line ."\n";
00163     } while (! feof($p["std_out"]) );;
00164     echo "stderr\n";
00165     do {
00166         $line = fgets($p["std_err"], 1024);
00167         echo ">> ".$line."\n";
00168     } while (! feof($p['std_err']));;
00169 
00170     $rmt->ssh_close($p);
00171 }
00172 
00173 #$debug_popen = TRUE;
00174 if (isset($debug_popen)) {
00175     $cout = $rmt->ssh_popen("ls -C", "r");
00176     echo fread($cout, 8192);
00177     $rmt->ssh_pclose($cout);
00178     echo "\n";
00179     /*
00180     $cin = $rmt->ssh_popen("/usr/sbin/Mail -s test jr", "w");
00181        fputs($cin, "\nTest");
00182        fputs($cin, ".\n");
00183        fputs($cin, "\004");
00184     $rmt->ssh_pclose($cin);
00185     */
00186 }
00187 
00188 $rmt->destruct();
00189 
00190 
00191 echo "</pre>\n\n";
00192 exit;
00193 
00194 ?>

Generated on Thu Jun 16 11:03:56 2005 for SExec by doxygen 1.3.6