forked from bestnetwork/Telnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
to be able to handle byte-streams
- Loading branch information
Showing
2 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
* Matthias Blaser <[email protected]> | ||
* | ||
* Extended by Christian Hammers <[email protected]> | ||
* | ||
* and Igor Scheller <[email protected]> | ||
*/ | ||
class TelnetClient { | ||
|
||
|
@@ -24,20 +24,25 @@ class TelnetClient { | |
|
||
protected $socket = NULL; | ||
protected $buffer = NULL; | ||
protected $globalBuffer = NULL; | ||
protected $prompt; | ||
protected $errPrompt; | ||
protected $errNo; | ||
protected $errStr; | ||
|
||
protected $NULL; | ||
protected $CR; | ||
protected $DC1; | ||
protected $WILL; | ||
protected $WONT; | ||
protected $DO; | ||
protected $DONT; | ||
protected $IAC; | ||
|
||
protected $globalBuffer = ''; | ||
/** | ||
* @var bool Is binary mode enabled? | ||
*/ | ||
protected $binaryMode = false; | ||
|
||
/** | ||
* Constructor. Initialises host, port and timeout parameters | ||
|
@@ -129,15 +134,34 @@ public function disconnect(){ | |
* @param string $command Command to execute | ||
* @param null|string $prompt | ||
* @param null|string $errPrompt | ||
* @return string Command result | ||
* @return string|bool Command result or true in binary mode | ||
* @throws TelnetException | ||
*/ | ||
public function execute( $command, $prompt = NULL, $errPrompt = NULL ){ | ||
if($this->binaryMode){ | ||
$this->executeBlind($command); | ||
return true; | ||
} | ||
|
||
$this->write($command); | ||
$this->read($prompt, $errPrompt); | ||
return $this->getBuffer(); | ||
} | ||
|
||
/** | ||
* Executes the given command without output | ||
* | ||
* @param string $command | ||
* @param bool $addNewLine | ||
*/ | ||
public function executeBlind($command, $addNewLine = true){ | ||
$buffer = $this->buffer; | ||
|
||
$this->write($command, $addNewLine); | ||
|
||
$this->buffer = $buffer; | ||
} | ||
|
||
/** | ||
* Attempts login to remote host. | ||
* This method is a wrapper for lower level private methods and should be | ||
|
@@ -204,6 +228,22 @@ public function clearBuffer(){ | |
$this->buffer = ''; | ||
} | ||
|
||
/** | ||
* Returns true if binary mode is enabled | ||
*/ | ||
public function getBinaryMode(){ | ||
return $this->binaryMode; | ||
} | ||
|
||
/** | ||
* Enables the binary mode | ||
* | ||
* @param bool $binaryMode | ||
*/ | ||
public function setBinaryMode( $binaryMode = true ){ | ||
$this->binaryMode = $binaryMode; | ||
} | ||
|
||
/** | ||
* Reads characters from the socket and adds them to command buffer. | ||
* Handles telnet control characters. Stops when prompt is encountered. | ||
|
@@ -260,6 +300,46 @@ protected function read( $prompt = NULL, $errPrompt = NULL ){ | |
} | ||
|
||
}while( $c != $this->NULL || $c != $this->DC1 ); | ||
|
||
return ''; | ||
} | ||
|
||
/** | ||
* Reads the given amount of bytes | ||
* | ||
* @param int $count | ||
* @return string | ||
* @throws TelnetException | ||
*/ | ||
public function readBytes( $count ){ | ||
if( !$this->socket ){ | ||
throw new TelnetException('Telnet connection closed'); | ||
} | ||
|
||
// clear the buffer | ||
$this->clearBuffer(); | ||
|
||
while( $count ){ | ||
$c = $this->getc(); | ||
|
||
if( $c === false ){ | ||
throw new TelnetException('Couldn\'t find the requested "' . $count . '" bytes, it was not in the data returned from server: ' . $this->buffer); | ||
} | ||
|
||
// Interpreted As Command | ||
if( $c == $this->IAC && !$this->binaryMode ){ | ||
if( $this->negotiateTelnetOptions() ){ | ||
continue; | ||
} | ||
} | ||
|
||
// append current char to global buffer | ||
$this->buffer .= $c; | ||
|
||
$count -= strlen($c); | ||
} | ||
|
||
return $this->buffer; | ||
} | ||
|
||
/** | ||
|
@@ -312,9 +392,16 @@ public function getGlobalBuffer(){ | |
/** | ||
* Telnet control character magic | ||
* | ||
* @returns bool | ||
* @throws TelnetException | ||
*/ | ||
protected function negotiateTelnetOptions(){ | ||
if( $this->binaryMode ){ | ||
$b = $this->getGlobalBuffer(); | ||
$this->buffer .= substr($b, -1); | ||
return; | ||
} | ||
|
||
$c = $this->getc(); | ||
|
||
if( $c != $this->IAC ){ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,10 @@ | |
{ | ||
"name": "Christian Hammers", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Igor Scheller", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
|
@@ -40,4 +44,3 @@ | |
} | ||
} | ||
} | ||
|