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 2022 by niro, Mon May 9 22:13:37 2011 UTC revision 2194 by niro, Tue Jun 7 13:10:02 2011 UTC
# Line 1  Line 1 
1  <?  <?
2   include("config.inc.php");  include('config.inc.php');
3    include('ppping.inc.php');
4    
5   function exec_on_client($serial, $ip, $cmd, $failure=1, $verbose=0)  function sshdo($cmd, $ip, $user="root")
6   {  {
7   global $pingcmd;   global $privkey_file;
8   global $sshcmd;   global $pubkey_file;
9    
10     if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist. You must install the ssh2 module from pecl library!");
11    
12   $pubkey = mysql_query('select public_key from ssh_auth_clients where serial='.$serial.'');   if(!($con = ssh2_connect($ip, "22")))
13   while ($row = mysql_fetch_row ($pubkey))   {
14     echo "failure, could not connect\n";
15     }
16     else
17     {
18     if(!ssh2_auth_pubkey_file($con, $user, $pubkey_file, $privkey_file))
19   {   {
20   # write known hosts   echo "failure, could not login\n";
21   $hostfile = fopen ($home."/.ssh/known_hosts","w");   }
22   fwrite($hostfile,$ip." ".$row[0],strlen($row[0])+strlen($ip)+1);   else
23   fclose($hostfile);   {
24     echo "logged in\n";
  # check if the client is online  
  passthru($pingcmd.' '.$ip.' &> /dev/null && exit 0 || exit 1', $retval);  
  if ($retval == 0)  
  {  
  if ($verbose == 1) echo "sshcmd: '".$sshcmd."'<br>";  
25    
26   # exec the cmd   if(!($stream = ssh2_exec($con, "source /etc/profile; ".$cmd."; echo '__RETVAL__:'$?")))
27   exec($sshcmd.' '.$ip.' "source /etc/profile;'.$cmd.'"', $cmdout, $err);   {
28   if ($verbose == 1)   echo "failure, could not execute command '".$cmd."'\n";
29     }
30     else
31     {
32     // collect stream data
33     stream_set_blocking($stream, true);
34     $data = "";
35     while ($buf =  fread($stream,4096))
36   {   {
37   echo "err: '".$err."'<br>";   $data .= $buf;
38   foreach( $cmdout as $i )   echo "buf=" .$buf."\n";
  {  
  echo "cmdout: '".$i."'<br>";  
  }  
39   }   }
40     fclose($stream);
41   if($err != 0) if ($failure == 1) echo '<script language="JavaScript">alert(\'Cmd "'.$cmd.'" *failed* on client #'.$serial.' .\')</script>';   # get the last element of data as retval
42   else if ($verbose == 1) echo '<script language="JavaScript">alert(\'Cmd "'.$cmd.'" successfully run on client #'.$serial.' .\')</script>';   $retval = explode('__RETVAL__:', trim($data));
43   }   }
  else if ($failure == 1) echo '<script language="JavaScript">alert(\'Client #'.$serial.' with IP "'.$ip.'" is offline. Please try again later...\')</script>';  
44   }   }
   
  # return the output of $cmd  
  return $cmdout;  
45   }   }
46    
47     // return an array -> 0=retval, 1=data
48     return array($retval[1], $retval[0]);
49    }
50    
51    function ssh_editvar($var, $value, $file)
52    {
53     global $ip;
54    
55     // sed cmd: ssh -l root 10.11.12.13 'sed -i -e "s|^\(VAR=\).*|\1\"VALUE\"| FILE'
56     sshdo('if [[ ! -z $(grep "^'.$var.'=" '.file.') ]]; then sed -i -e "s|^\('.$var.'=\).*|\1\"'.$value.'\"|" '.$file. 'else echo "'.$var.'=\"'.$value.'\"" >> '.file, $ip);
57    }
58    
59    function online($ip)
60    {
61     $ping = new PPPing();
62     $ping->hostname = $ip;
63     $ping->timeout = "0.3";
64     $result = $ping->Ping();
65    
66     if($result<0)
67     {
68     echo "error: ".$ping->strError($result);
69     $retval=1;
70     }
71     else
72     {
73     echo $result." ms";
74     $retval=0;
75     }
76     return $retval;
77    }
78  ?>  ?>

Legend:
Removed from v.2022  
changed lines
  Added in v.2194