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!
// +----------------------------------------------------------------------+ // | 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(); }
// 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($command, false, false, '@'); $stream = $this->execCommand($command, true, $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($command, true, $get_stderr); $command .= ' | b64encode - | sed 1d | sed \'$d\' '; $command .= ' ; echo \'@\'; '; $stream = $this->execCommand($command, true, $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($command, true, $get_stderr); $stream = $this->execCommand($command, true, $pty, $env); $buf = ''; while (!$this->feof()) $buf .= $this->getStreamOutput(); if ($this->_log_reads) $this->_log_buf .= $buf; return $buf; }
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(); }
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($stream, SSH2_STREAM_STDERR); if ($set_blocking) stream_set_blocking($err_stream, true); return $err_stream; }
Search engine optimization is not only the job of a marketing staff. It must be considered from a web site's inception and throughout its lifetime by the web site developer. Professional Search Engine Optimization with PHP provides developers with the information they need to create and maintain a search engine friendly web site, and avoid common pitfalls that confuse search engine spiders.