Accelerate your eCommerce ambitions with adeptCommerce Suite

SEO Egghead Consulting Group is a web development firm dedicated to creating custom, search-engine-optimized web site applications.

We specialize in eCommerce and content management web sites that not only render information beautifully to the human, but also satisfy the "third browser" - the search engine. To us, search engines are people too.

image description image description image description image description

SSH2 PHP Wrappers and 'How To' Examples

Prevents painful hair-pulling when using ssh2_* libraries via PHP ...

NEED A GREAT WEB SITE? NEED IT TO BE SEARCH-ENGINE-FRIENDLY?

SEO Egghead is a web development firm dedicated to creating custom, search engine optimized web site applications. We specialize in eCommerce and content management web sites that not only render information beautifully to the human, but also satisfy the "third browser" — the search engine. To us, search engines are people too. Click here to talk to us. We'd love to help!
X
More information on this library — http://www.seoegghead.com/blog/seo/ssh2-php-howto-guide-ssh-connections-made-easy-in-php-p343.html.

Copy the following program to your PHP application folder.



Example Use:


include('SSH2.php');
$s = new SSH2('localhost');
$s->loginWithPassword('MYLOGIN', 'MYPASSWORD');
echo $s->execCommandBlock('find /cat/food');
exit();



<?php

// +----------------------------------------------------------------------+
// | SSH2 1.3                                                             |
// | Wrapper to use SSH from PHP                                          |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004-2008                                              |
// | SEO Egghead, Inc.                                                    |
// | http://www.seoegghead.com/                                           |
// |                                                                      |
// | This program is free software; you can redistribute it and/or        |
// | modify it under the terms of the GNU General Public License          |
// | as published by the Free Software Foundation; either version 2       |
// | of the License, or (at your option) any later version.               |
// |                                                                      |
// | This program is distributed in the hope that it will be useful,      |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
// | GNU General Public License for more details.                         |
// |                                                                      |
// | You should have received a copy of the GNU General Public License    |
// | along with this program; if not, write to the Free Software          |
// | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA        |
// +----------------------------------------------------------------------+

class SSH2
{
    
    var 
$_host;
    var 
$_port;
    
    var 
$_username;
    var 
$_password;
    
    var 
$_c;
    
    var 
$_current_stream;
    
    var 
$_sftp;
    
    var 
$_log_reads false;
    var 
$_log_writes false;
    
    var 
$_log_buf '';
    
    function 
SSH2($host$port 22$callbacks = array())
    {
        if (!
function_exists('ssh2_connect')) {
            echo 
'ERROR: PECL ssh2 must be installed!';
            die();
        }
        
        
$this->_host $host;
        
$this->_port $port;
        
$this->_c ssh2_connect($this->_host$this->_port, array(), $callbacks);
    }

    function 
setLogReads($setting true)
    {
        
$this->_log_reads $setting;
    }
    
    function 
setLogWrites($setting true)
    {
        
$this->_log_writes $setting;        
    }
        
    function 
loginWithPassword($username$password)
    {
        return 
ssh2_auth_password($this->_c$username$password);
    }

    
// WARNING: Blocking only really works as expected if reading data afterwards. 
    
function execCommand($command$set_blocking false$pty null,
                         
$env = array())
    {
        if (!
$pty$pty null;
        
$stream ssh2_exec($this->_c$command$pty$env);
        
$this->_current_stream $stream;
        if (
$set_blockingstream_set_blocking($streamtrue);
        return 
$stream;
    }
    
    function 
_generateCommand($command$get_stdout true$get_stderr false,
                              
$append_output '')
    {
        
$command ' ( ' $command ' ) ';
        
        if (
$get_stdout && $get_stderr) {
            
$command .= ' 2>&1 ';
        } elseif (
$get_stdout && !$get_stderr) {
            
$command .= ' 2>/dev/null ';
        } elseif (!
$get_stdout && $get_stderr) {
            
$command  ' ( ' $command ' 1>/dev/null ) 2>&1 '
        } else {
            
$command .= ' >/dev/null 2>&1 ';
        }
        
        
$command ' sh -c ' escapeshellarg($command);
        if (
$append_output$command .= ' ; echo ' 
                            
escapeshellarg($append_output) . ' ; ';
        return 
$command;
    }
    
    
// Use this if you want to wait until the command is executed.
    
function execCommandBlockNoOutput($command$not_used true$pty null,
                                      
$env = array())
    {    
        
$command SSH2::_generateCommand($commandfalsefalse'@');        
        
$stream $this->execCommand($commandtrue$pty$env);
        
$this->waitPrompt('@');        
        return 
$stream;
    }
    
    
// Use this if you want to wait until the command is executed and want the output.
    // This is an old implementation of execCommandBlockING(); it has a b64encode dependency.
    
function execCommandBlock($command$not_used true$pty null,
                              
$env = array(), $get_stderr false)
    {    
        
$command SSH2::_generateCommand($commandtrue$get_stderr);
        
$command .= ' | b64encode - | sed 1d | sed \'$d\' ';
        
$command .= ' ; echo \'@\'; ';
        
$stream $this->execCommand($commandtrue$pty$env);
        
$this->waitPrompt('@'$_buf);        
        return 
base64_decode($_buf);    
    }
        
    
// Use this if you want to wait until the command is executed and want the output.
    
function execCommandBlocking($command$not_used true$pty null,
                                 
$env = array(), $get_stderr false)
    {    
        
$command SSH2::_generateCommand($commandtrue$get_stderr);
        
$stream $this->execCommand($commandtrue$pty$env);
        
$buf ''; while (!$this->feof()) $buf .= $this->getStreamOutput();
        if (
$this->_log_reads$this->_log_buf .= $buf;        
        return 
$buf;
    }    
    
    function 
getShell($set_blocking false$term_type 'vt102',
                      
$env = array(), $width null$height null)
    {
        
$stream ssh2_shell($this->_c$term_type$env$width$height,
                            (
$width && $height) ? SSH2_TERM_UNIT_CHARS null);
        
$this->_current_stream $stream;
        if (
$set_blockingstream_set_blocking($streamtrue);
        return 
$stream;
    }
        
    function 
waitPrompt($prompt_regex '> $', &$buf ''
                        
$timeout_secs 0$stream null)
    {
        if (!
$stream$stream $this->_current_stream;
        if (
$timeout_secs) {
            
            
$_ver preg_replace('#-.*?$#'''phpversion('ssh2'));
            if (
version_compare($_ver'0.11.0''<')) {
                echo 
"ERROR: Using old version of PECL ssh2 ($_ver); timeouts broken!";
                die();
            }
            
            
$end time() + $timeout_secs;            

            
$saved_meta_info $this->getMeta($stream);
            
stream_set_blocking($streamfalse);

            while (!
$_r preg_match("#$prompt_regex#"$buf .= fread($stream4096))) {        
                if (
time() > $end) break;
                
fflush($stream);
                
usleep(1); 
            }
            
            
stream_set_blocking($stream$saved_meta_info['blocked']);
            
        } else {
            while (!
$_r preg_match("#$prompt_regex#"$buf .= fread($stream4096)))        
                
fflush($stream); 
        }

        if (
$this->_log_reads$this->_log_buf .= $buf;    
        return 
$_r;
    }

    function 
writePrompt($command$add_newline true$stream null)
    {
        if (!
$stream$stream $this->_current_stream;
        
fflush($stream);
        
$_command = ($command . ($add_newline "\n" ''));
        
$num_bytes fwrite($stream$_command);
        
fflush($stream);
        if (
$this->_log_writes$this->_log_buf .= substr($_command0$num_bytes);            
        return 
$num_bytes;
    }    
    
    function 
getStreamOutput($length 4096$stream null)
    {
        if (!
$stream$stream $this->_current_stream;
        
$buf fread($stream$length);
        if (
$this->_log_reads$this->_log_buf .= $buf;        
        return 
$buf;
    }
    
    
// WARNING: This may not necessarily get all data.
    
function getAllStreamOutput($stream null)
    {
        if (!
$stream$stream $this->_current_stream;
        
$buf stream_get_contents($stream);
        if (
$this->_log_reads$this->_log_buf .= $buf;        
        return 
$buf;
    }
    
    function 
closeStream($stream null)
    {
        if (!
$stream$stream $this->_current_stream;
        return 
fclose($stream);
    }
    
    function 
fetchSTDERR($set_blocking false$stream null)
    {
        if (!
$stream$stream $this->_current_stream;
        
$err_stream ssh2_fetch_stream($streamSSH2_STREAM_STDERR);
        if (
$set_blockingstream_set_blocking($err_streamtrue);
        return 
$err_stream;
    }
    
    function 
SCPSend($local_file$remote_file$create_mode null)
    {
        return 
ssh2_scp_send($this->_c$local_file$remote_file$create_mode);
    }
        
    function 
sendContents($file_contents$remote_file$create_mode null,
                          
$set_blocking false)
    {
        
$fp fopen("ssh2.sftp://" $this->_sftp "$remote_file"$create_mode);
        if (
$set_blockingstream_set_blocking($fptrue);
        return 
fwrite($fp$file_contentsstrlen($file_contents));
    }
    
    function 
sendStream($input_stream$remote_file$create_mode null,
                        
$set_blocking false)
    {
        
$fp fopen("ssh2.sftp://" $this->_sftp "$remote_file"$create_mode);
        if (
$set_blockingstream_set_blocking($fptrue);
        
$bytes stream_copy_to_stream($input_stream$fp);
        
fclose($fp);
        return 
$bytes;
    }
    
    function 
SCPReceive($remote_file$local_file)
    {
        return 
ssh2_scp_recv($this->_c$remote_file$local_file);
    }
    
    function 
openSFTP()
    {
        
$this->_sftp ssh2_sftp($this->_c);
    }
    
    function 
unlink($filename)
    {
        return 
ssh2_sftp_unlink($this->_sftp$filename);
    }

    function 
feof($stream null)
    {
        if (!
$stream$stream $this->_current_stream;
        return 
feof($stream);        
    }
    
    function 
getMeta($stream null)
    {
        if (!
$stream$stream $this->_current_stream;
        return 
stream_get_meta_data($stream);
    }
    
    
// Use file wrappers for other functionality.
    
}

?>


Tell an amigo:
Sphinn Digg Reddit del.icio.us StumbleUpon Facebook