Magellan Linux

Diff of /alx-src/branches/alx-web-060/include/common-functions.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3342 by niro, Tue Jul 5 15:35:52 2011 UTC revision 3343 by niro, Wed Feb 1 10:48:54 2012 UTC
# Line 6  function sshdo($cmd, $ip, $user="root") Line 6  function sshdo($cmd, $ip, $user="root")
6  {  {
7   global $privkey_file;   global $privkey_file;
8   global $pubkey_file;   global $pubkey_file;
9     global $socket_timeout;
10    
11   if (!function_exists("ssh2_connect")) die("sshdo(): function ssh2_connect doesn't exist. You must install the ssh2 module from pecl library!");   if (!function_exists("ssh2_connect")) die("sshdo(): function ssh2_connect doesn't exist. You must install the ssh2 module from pecl library!");
12    
13   if(!($con = ssh2_connect($ip, "22")))   // check if port exists
14     $con = fsockopen( $ip, "22", $errono, $errstr, $socket_timeout);
15     if (!$con)
16   {   {
17   echo "sshdo(): failure, could not connect\n";   echo "sshdo(): socket failure,<br>could not connect to port 22<br>";
18   }   }
19   else   else
20   {   {
21   if(!ssh2_auth_pubkey_file($con, $user, $pubkey_file, $privkey_file))   // first off all close our open socket
22     fclose($con);
23    
24     if(!($con = ssh2_connect($ip, "22")))
25   {   {
26   echo "sshdo(): failure, could not login\n";   echo "sshdo(): failure, could not connect<br>";
27   }   }
28   else   else
29   {   {
30   //echo "logged in\n";   if(!ssh2_auth_pubkey_file($con, $user, $pubkey_file, $privkey_file))
   
  if(!($stream = ssh2_exec($con, "source /etc/profile; ".$cmd."; echo '__RETVAL__:'$?")))  
31   {   {
32   echo "sshdo(): failure, could not execute command '".$cmd."'\n";   echo "sshdo(): failure, could not login<br>";
33   }   }
34   else   else
35   {   {
36   // collect stream data   //echo "logged in\n";
37   stream_set_blocking($stream, true);  
38   $data = "";   if(!($stream = ssh2_exec($con, "source /etc/profile; ".$cmd."; echo '__RETVAL__:'$?")))
39   while ($buf =  fread($stream,4096))   {
40     echo "sshdo(): failure, could not execute command '".$cmd."'<br>";
41     }
42     else
43   {   {
44   $data .= $buf;   // collect stream data
45   //echo "buf=" .$buf."\n";   stream_set_blocking($stream, true);
46     $data = "";
47     while ($buf =  fread($stream,4096))
48     {
49     $data .= $buf;
50     //echo "buf=" .$buf."\n";
51     }
52     fclose($stream);
53     # get the last element of data as retval
54     $retval = explode('__RETVAL__:', trim($data));
55   }   }
  fclose($stream);  
  # get the last element of data as retval  
  $retval = explode('__RETVAL__:', trim($data));  
56   }   }
57   }   }
58   }   }

Legend:
Removed from v.3342  
changed lines
  Added in v.3343