ShellCmd Class Reference
[General]

Functions to execute shell commands. More...

List of all members.

Static Public Member Functions

static exec ($command_, $parameter_=NULL)
 Execute any shell command.
static sshExec ($remoteServer_, $command_)
 Execute a shell command on a remoteserver, through SSH.
static sshGet ($remoteServer_, $from_, $to_)
 Get a file from a remote server through SSH.
static sshPut ($remoteServer_, $from_, $to_)
 Put a file on a remote server through SSH.


Detailed Description

Functions to execute shell commands.

Author:
Daniel Lindh <[email protected]>
Todo:
: Use escapeshellarg() or escapeshellcmd() to ensure that users cannot trick the system into executing arbitrary commands.

Definition at line 10 of file ShellCmd.class.php.


Member Function Documentation

static ShellCmd::exec ( command_,
parameter_ = NULL 
) [static]

Execute any shell command.

If any error occurs an exception will be sent.

 try
 {
   ShellCmd::exec('whoami');
   ShellCmd::exec("wc", "I may be synthetic, but I\'m not stupid.\n\n");
 }
 catch (Exception $e)
 {
   return $e->getMessage();
 }

Parameters:
$command_ - string - The command that should be executed.
$parameter_ - string - Any parameter that should be sent to the shell input, after the command has been executed. Ie. A password after a login command has been executed
Returns:
- string - The output generated by the command.

Definition at line 40 of file ShellCmd.class.php.

Referenced by sshExec(), sshGet(), sshPut(), and UTShellCmd::testExec().

00041   {
00042     $retval = '';
00043     $error = '';
00044 
00045     $descriptorspec = array
00046     (
00047       0 => array('pipe', 'r'),
00048       1 => array('pipe', 'w'),
00049       2 => array('pipe', 'w')
00050     );
00051     $pipes = array();
00052 
00053     $resource = proc_open($command_, $descriptorspec, $pipes, null, $_ENV);
00054     if (is_resource($resource))
00055     {
00056       $stdin = $pipes[0];
00057       $stdout = $pipes[1];
00058       $stderr = $pipes[2];
00059 
00060       // Write to standard in
00061       if (!empty($parameter_))
00062       {
00063         fwrite($stdin, $parameter_);
00064       }
00065       fclose($stdin);
00066 
00067       // Get result from command
00068       while (!feof($stdout))
00069       {
00070         $retval .= fgets($stdout);
00071       }
00072       fclose($stdout);
00073 
00074       // Get the errors back, if any
00075       while (!feof($stderr))
00076       {
00077         $error .= fgets($stderr);
00078       }
00079       fclose($stderr);
00080 
00081       $exitCode =  proc_close($resource);
00082       if (0 != $exitCode)
00083       {
00084         $error .= ' Exit code: ' . $exitCode;
00085       }
00086     }
00087 
00088     if (! empty($error))
00089     {
00090       throw new Exception($error);
00091     }
00092     else
00093     {
00094       return $retval;
00095     }
00096   }

static ShellCmd::sshExec ( remoteServer_,
command_ 
) [static]

Execute a shell command on a remoteserver, through SSH.

Parameters:
$remoteServer_ - string - Login and servername of remote server Ie. [email protected]
$command_ - string - The command that should be executed on the remote server.

Definition at line 108 of file ShellCmd.class.php.

References exec().

Referenced by UTShellCmd::testSshExec().

00109   {
00110     return ShellCmd::exec('/usr/bin/ssh ' . $remoteServer_ . ' "' . $command_ . '"');
00111   }

static ShellCmd::sshGet ( remoteServer_,
from_,
to_ 
) [static]

Get a file from a remote server through SSH.

Parameters:
$remoteServer_ Login and servername of remote server Ie. [email protected]
$from_ Directory and file on remote server that should be copied. Ie. /home/fareoffice/file.txt
$to_ Directory and file on local server where the file should be copied to. Ie. /home/fareoffice/LocalFile.txt

Definition at line 129 of file ShellCmd.class.php.

References exec().

00130   {
00131     ShellCmd::exec('/usr/bin/scp ' . $remoteServer_ . ':' . $from_ . ' ' . $to_);
00132   }

static ShellCmd::sshPut ( remoteServer_,
from_,
to_ 
) [static]

Put a file on a remote server through SSH.

Parameters:
$remoteServer_ Login and servername of remote server Ie. [email protected]
$from_ Directory and file on local server that should be copied. Ie. /home/fareoffice/LocalFile.txt
$to_ Directory and file on remote server where the file should be placed. Ie. /home/fareoffice/file.txt

Definition at line 150 of file ShellCmd.class.php.

References exec().

00151   {
00152     ShellCmd::exec('/usr/bin/scp ' . $from_ . ' ' . $remoteServer_ .':' . $to_);
00153   }


The documentation for this class was generated from the following file:

Generated on Wed May 6 23:28:24 2009 for fareofficelib by  doxygen 1.5.8