added Net_SSH2::getLog()

git-svn-id: http://phpseclib.svn.sourceforge.net/svnroot/phpseclib/trunk@27 21d32557-59b3-4da0-833f-c5933fad653e
This commit is contained in:
Jim Wigginton 2009-03-25 22:29:42 +00:00
parent c369990f77
commit f65f2983b7

View File

@ -41,7 +41,7 @@
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVII Jim Wigginton * @copyright MMVII Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt * @license http://www.gnu.org/licenses/lgpl.txt
* @version $Id: SSH2.php,v 1.11 2009-03-23 22:44:51 terrafrost Exp $ * @version $Id: SSH2.php,v 1.12 2009-03-25 22:29:42 terrafrost Exp $
* @link http://phpseclib.sourceforge.net * @link http://phpseclib.sourceforge.net
*/ */
@ -87,6 +87,20 @@ define('NET_SSH2_MASK_CONSTRUCTOR', 0x00000001);
define('NET_SSH2_MASK_LOGIN', 0x00000002); define('NET_SSH2_MASK_LOGIN', 0x00000002);
/**#@-*/ /**#@-*/
/**#@+
* @access public
* @see Net_SSH2::getLog()
*/
/**
* Returns the message numbers
*/
define('NET_SSH2_LOG_SIMPLE', 1);
/**
* Returns the message content
*/
define('NET_SSH2_LOG_COMPLEX', 2);
/**#@-*/
/** /**
* Pure-PHP implementation of SSHv2. * Pure-PHP implementation of SSHv2.
* *
@ -400,6 +414,24 @@ class Net_SSH2 {
*/ */
var $get_seq_no = 0; var $get_seq_no = 0;
/**
* Message Number Log
*
* @see Net_SSH2::getLog()
* @var Array
* @access private
*/
var $message_number_log = array();
/**
* Message Log
*
* @see Net_SSH2::getLog()
* @var Array
* @access private
*/
var $message_log = array();
/** /**
* Default Constructor. * Default Constructor.
* *
@ -1376,6 +1408,11 @@ class Net_SSH2 {
$this->get_seq_no++; $this->get_seq_no++;
if (defined('NET_SSH2_LOGGING')) {
$this->message_number_log[] = '<- ' . $this->message_numbers[ord($payload[0])];
$this->message_log[] = $padding;
}
return $this->_filter($payload); return $this->_filter($payload);
} }
@ -1483,6 +1520,11 @@ class Net_SSH2 {
return false; return false;
} }
if (defined('NET_SSH2_LOGGING')) {
$this->message_number_log[] = '-> ' . $this->message_numbers[ord($data[0])];
$this->message_log[] = $data;
}
// 4 (packet length) + 1 (padding length) + 4 (minimal padding amount) == 9 // 4 (packet length) + 1 (padding length) + 4 (minimal padding amount) == 9
$packet_length = strlen($data) + 9; $packet_length = strlen($data) + 9;
// round up to the nearest $this->encrypt_block_size // round up to the nearest $this->encrypt_block_size
@ -1569,13 +1611,57 @@ class Net_SSH2 {
} }
} }
/**
* Returns a log of the packets that have been sent and received.
*
* $type can be either NET_SSH2_LOG_SIMPLE or NET_SSH2_LOG_COMPLEX. NET_SSH2_LOG_COMPLEX
* will contain your severs password, so don't distribute log files produced with it unless
* you've redacted the password.
*
* @param Integer $type
* @access public
* @return String or Array
*/
function getLog($type = NET_SSH2_LOG_SIMPLE)
{
if ($type != NET_SSH2_LOG_COMPLEX) {
return $this->message_number_log;
}
$boundary = ':';
$long_width = 65;
$short_width = 15;
$output = '';
for ($i = 0; $i < count($this->message_log); $i++) {
$output.= $this->message_number_log[$i] . "\r\n";
do {
$fragment = $this->_string_shift($this->message_log[$i], $short_width);
$hex = substr(
preg_replace(
'#(.)#es',
'"' . $boundary . '" . str_pad(dechex(ord(substr("\\1", -1))), 2, "0", STR_PAD_LEFT)',
$fragment),
strlen($boundary)
);
// replace non ASCII printable characters with dots
// http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
$raw = preg_replace('#[^\x20-\x7E]#', '.', $fragment);
$output.= str_pad($hex, $long_width - $short_width, ' ') . $raw . "\r\n";
} while (!empty($this->message_log[$i]));
$output.= "\r\n";
}
return $output;
}
/** /**
* Returns Debug Information * Returns Debug Information
* *
* If any debug information is sent by the server, this function can be used to access it. * If any debug information is sent by the server, this function can be used to access it.
* *
* @return String; * @return String
* @access private * @access public
*/ */
function getDebugInfo() function getDebugInfo()
{ {