Magellan Linux

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

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

revision 8509 by niro, Mon Sep 7 10:50:48 2015 UTC revision 8510 by niro, Mon Feb 8 09:16:29 2016 UTC
# Line 2  Line 2 
2  include('config.inc.php');  include('config.inc.php');
3  include('ppping.inc.php');  include('ppping.inc.php');
4    
 function sshdo($cmd, $ip, $user="root")  
 {  
  global $privkey_file;  
  global $pubkey_file;  
  global $socket_timeout;  
   
  if (!function_exists("ssh2_connect")) die("sshdo(): function ssh2_connect doesn't exist. You must install the ssh2 module from pecl library!");  
   
  // check if port exists  
  $con = fsockopen( $ip, "22", $errono, $errstr, $socket_timeout);  
  if (!$con)  
  {  
  echo "sshdo(): socket failure,<br>could not connect to port 22<br>";  
  }  
  else  
  {  
  // first off all close our open socket  
  fclose($con);  
   
  if(!($con = ssh2_connect($ip, "22")))  
  {  
  echo "sshdo(): failure, could not connect<br>";  
  }  
  else  
  {  
  if(!ssh2_auth_pubkey_file($con, $user, $pubkey_file, $privkey_file))  
  {  
  echo "sshdo(): failure, could not login<br>";  
  }  
  else  
  {  
  //echo "logged in\n";  
   
  if(!($stream = ssh2_exec($con, "source /etc/profile; ".$cmd."; echo '__RETVAL__:'$?")))  
  {  
  echo "sshdo(): failure, could not execute command '".$cmd."'<br>";  
  }  
  else  
  {  
  // collect stream data  
  stream_set_blocking($stream, true);  
  $data = "";  
  while ($buf =  fread($stream,4096))  
  {  
  $data .= $buf;  
  //echo "buf=" .$buf."\n";  
  }  
  fclose($stream);  
  # get the last element of data as retval  
  $retval = explode('__RETVAL__:', trim($data));  
  }  
  }  
  }  
  }  
   
  // return an array -> 0=retval, 1=data  
  return array($retval[1], $retval[0]);  
 }  
   
 function ssh_editvar($var, $value, $file)  
 {  
  global $ip;  
   
  // sed cmd: ssh -l root 10.11.12.13 'sed -i -e "s|^\(VAR=\).*|\1\"VALUE\"| FILE'  
  sshdo('if [[ ! -z $(grep "^'.$var.'=" '.$file.') ]]; then sed -i -e "s|^\('.$var.'=\).*|\1\"'.$value.'\"|" '.$file.' ;else echo "'.$var.'=\"'.$value.'\"" >> '.$file.'; fi', $ip);  
 }  
   
5  function online($ip)  function online($ip)
6  {  {
7   $ping = new PPPing();   $ping = new PPPing();

Legend:
Removed from v.8509  
changed lines
  Added in v.8510