Cradle  0.3.3
Simple library for creating Web-based applications
Telnet Class Reference

Telnet interaction. More...

Inheritance diagram for Telnet:
RemoteExecutor

Public Member Functions

 open ($config=null)
 
 close ()
 
 getInputPrompt ()
 
 getInputBuffer ($trimmed=null)
 
 getLastTimeout ()
 
 getBanner ()
 
- Public Member Functions inherited from RemoteExecutor
 __construct ($config=null)
 
 __destruct ()
 
 open ($config=null)
 
 close ()
 
 exec ($command, $timeout=null)
 
 getLastError ()
 
 getErrors ()
 
 getDebug ()
 
 clearBuffers ($type= 'all')
 
 isAlive ($timeout=null)
 

Data Fields

const ERR_SOCKET = 5
 
const ERR_CLOSED_BY_REMOTE = 6
 
const ERR_TIMEOUT = 7
 
const ERR_TIMEOUT_EXEC = 8
 
 $port = 23
 
 $timeout = 10
 
 $trimResponse = true
 
 $terminalEchoOn = false
 
 $terminalGoAhead = true
 
 $ternimalType = "DEC-VT100"
 
 $terminalWidth = 0
 
 $terminatHeight = 0
 
 $inputPromptTemplate = '/^[^#>\$\%]+[#>\$\%]\s*$/'
 
- Data Fields inherited from RemoteExecutor
const ERR_HOST_IS_EMPTY = 1
 
const ERR_UNABLE_TO_CONNECT = 2
 
const ERR_AUTH_FAIL = 3
 
 $host
 
 $port
 
 $user
 
 $password
 
 $timeout
 
 $errorSilent = true
 
 $debug = 1
 
 $error = 1
 

Protected Member Functions

 error ($code, $source="", $detail=null)
 
 debug ($message, $source="")
 
 handshake ()
 
 authenticate ()
 
 executeCommand ($command, $timeout=null)
 
 trimResponse ($responseData)
 
 send ($data)
 
 read ($timeout)
 
 getAnswer ($inputPromptTemplates, $analyzeMode=0, $timeout=null, $callbackFunctions=null, $failFunction=null)
 
 parseInput ($string)
 
 getCommResponse ($commBuffer)
 
 toString ($input, $mode=null)
 
 getTerminalCommandName ($code)
 
 getTerminalOptionName ($code)
 
- Protected Member Functions inherited from RemoteExecutor
 init ($config)
 
 error ($code, $source="", $message=null, $detail=null)
 
 debug ($message, $source="")
 
 connect ()
 
 authenticate ()
 
 prepareCommand ($command)
 
 executeCommand ($command, $timeout=null)
 

Protected Attributes

 $socket
 
 $isAuthenticated = false
 
 $banner
 
 $inputBuffer
 
 $inputPrompt
 
 $lastRequest
 
 $lastTimeout
 
 $tmCmd
 
 $tmCmdCodes = null
 
 $tmOption
 
 $tmOptionCodes = null
 
- Protected Attributes inherited from RemoteExecutor
 $connection
 
 $isAuthenticated = false
 
 $debugLogger
 
 $errorLogger
 

Detailed Description

Telnet interaction.

A simple class to send commands to remote host through the TELNET protocol.

TELNET protocol specification: http://tools.ietf.org/html/rfc854
TELNET option: http://www.iana.org/assignments/telnet-options/telnet-options.xhtml
TELNET terminals: http://www.iana.org/assignments/terminal-type-names/terminal-type-names.xhtml#terminal-type-names-1

Version
4.0
Author
Digger mrdig.nosp@m.ger@.nosp@m.sad-s.nosp@m.yste.nosp@m.ms.ru

Example of usage:

<?php
//--- 1. Short usage:
$r = (new Telnet([ 'host' => "host", 'user' => "user", 'password' => "password" ]))->exec("show clock");
print_r( $r );
//--- 2. Normal usage:
$t = new Telnet([
'host' => "host",
'user' => "username",
'password' => "password",
'debug' => "debug.dat", // to file (option)
]);
$commands = "show clock"; // Single command as a string
$commands = "show clock\nterminal length 0"; // Several commands separated by "\n"
$commands = ["show clock"]; // Single command as an array
$commands = ["terminal length 0", "show clock"]; // Several commands as an array
$r = $t->exec($commands);
print_r( $r );
if ($r === false) {
echo "Error: " . implode(" : ", $t->getLastError()) . "\n";
print_r($t->getErrors());
}
//--- 3. Advanced usage:
$t = new Telnet([
'host' => "host",
'user' => "username",
'password' => "password",
'timeout' => 30, // default timeout is 30 sec
'debug' => 2, // to STDIN (echo)
'errorSilent' => false, // enable exceptions
]);
$commands = ["terminal length 0", "show clock"];
if (!is_array($commands)) { $commands = [$cmd]; }
try {
foreach ($commands as $command) {
echo "command : $command\n";
$r = $t->exec($command, 20); // every command with own timeout 20 sec
print_r($r);
}
} catch (Exception $e) {
echo "Exception: " . $e->getMessage() . " Code: " . $e->getCode() . "\n";
echo "Error: " . implode(" : ", $t->getLastError()) . "\n";
print_r($t->getErrors());
}
$t->close();
//--- 4. Several targets:
$config = ['user' => "user", 'password' => "password", 'errorSilent' => false, 'debug' => 2];
$hosts = ['host1', 'host2', 'host3'];
$commands = ['terminal length 0', 'show clock'];
$t = new Telnet($config);
foreach ($hosts as $host) {
try {
echo "Host: $host\n";
$t->open($host);
$r = $t->exec($commands);
print_r($r);
} catch (Exception $e) {
echo "Exception: " . $e->getMessage() . " Code: " . $e->getCode() . "\n";
echo "Error: " . implode(" : ", $t->getLastError()) . "\n";
print_r($t->getErrors());
}
}
$t->close();

Member Function Documentation

authenticate ( )
protected

Authenticate the access to remote side

Returns
boolean TRUE - Authentication is success.
FALSE - Authentication is fail.
close ( )

Close the telnet connection

debug (   $message,
  $source = "" 
)
protected

Create a debug message

error (   $code,
  $source = "",
  $detail = null 
)
protected

Create an error

Exceptions
Exception
executeCommand (   $command,
  $timeout = null 
)
protected

Execute a single command on remote side

Returns
string The text data of response.
getAnswer (   $inputPromptTemplates,
  $analyzeMode = 0,
  $timeout = null,
  $callbackFunctions = null,
  $failFunction = null 
)
protected

Advanced read (read wrapper)

getBanner ( )

Returns a banner of remote side

Returns
string Banner text from remote side
getCommResponse (   $commBuffer)
protected

Create a string with TELNET commands for response to remote side

getInputBuffer (   $trimmed = null)

Returns the last received data from remote side

Parameters
boolean$trimmed(option) Default value = $trimResponse (
See also
$trimResponse)
Returns
string Last received data
getInputPrompt ( )

Returns a input prompt marker of remote side

Returns
string The marker
getLastTimeout ( )

Returns seconds of the last timeout

Returns
float Seconds of last timeout
getTerminalCommandName (   $code)
protected

TELNET command code to command name

getTerminalOptionName (   $code)
protected

TELNET option code to option name

handshake ( )
protected

First request to remote side

open (   $config = null)

Open new telnet connection

Parameters
array | string$configAn array of properties to initialize the class. If $config is a string it will be interpreted as a host (same as $config = [ 'host' => $config ]).
Returns
resource|false Resource ID of the open socket or FALSE on fail.
parseInput (   $string)
protected

Parses data and separate into TELNET-commands and text

read (   $timeout)
protected

Reads the response data from the socket. Parses response data and separates into commands and text data. Sends telnet commands to remote side if it will found in response.

Parameters
int$timeoutMaximum seconds to wait the response
Returns
string|false Text data of response or FALSE on fail.
Exceptions
Exception
  1. If timeout;
  2. If socket is closed by the remote side
  3. If socket error
send (   $data)
protected

Sends a data through the socket

Parameters
string$dataData to send
Returns
int|false Count of bytes sent or FALSE on fail
toString (   $input,
  $mode = null 
)
protected

Converts mixed data to string

trimResponse (   $responseData)
protected

Strip a garbage from response data

Parameters
string$responseDataResponse data
Returns
string Trimmed data

Field Documentation

$inputPromptTemplate = '/^[^#>\$\%]+[#>\$\%]\s*$/'

string Regular expression template to find a input prompt marker ('Ready for input') of remote side. By default it set as: '/^[^#>$%]+[#>$%]*$/'

$port = 23

int The target TCP port.

$terminalEchoOn = false

boolean Enable or disable echo.
TELNET option (1) "ECHO-ON" http://tools.ietf.org/html/rfc857

$terminalGoAhead = true

boolean Enable or disable suppressing transmission of the 'TELNET GO AHEAD' character.
TELNET option (3) "SUPPRESS-GO-AHEAD" http://tools.ietf.org/html/rfc858

$terminalWidth = 0

int Value of terminal characters in line.
TELNET option (31) "WINDOW-SIZE" http://tools.ietf.org/html/rfc1073

$terminatHeight = 0

int Value of terminal lines count.
TELNET option (31) "WINDOW-SIZE" http://tools.ietf.org/html/rfc1073

$ternimalType = "DEC-VT100"
$timeout = 10

int The socket default timeout in seconds. Defines how long should wait the response from remote side.

$tmCmd
protected
Initial value:
= array (
"SE" => 0xf0,
"SB" => 0xfa,
"WILL" => 0xfb,
"WONT" => 0xfc,
"DO" => 0xfd,
"DONT" => 0xfe,
"IAC" => 0xff,
)

array TELNET commands

$tmOption
protected
Initial value:
= array (
"ECHO" => 0x1,
"SUPG" => 0x3,
"TTYPE" => 0x18,
"WSIZE" => 0x1f,
)

array TELNET options

$trimResponse = true

boolean If set true, the data returned by exec method will be trimmed. The echo of the command will be deleted from the beginning and the marker 'Ready for input' of the remote side will be deleted from the end.


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