Telnet class

Used to execute remote commands via telnet connection Usess sockets functions and fgetc() to process result

All methods throw Exceptions on error

Written by Dalibor Andzakovic dali@swerve.co.nz Based on the code originally written by Marc Ennaji and extended by Matthias Blaser mb@adfinis.ch

package Core

 Methods

Constructor. Initialises host, port and timeout parameters defaults to localhost port 23 (standard telnet port)

__construct(string $host = '127.0.0.1', integer $port = '23', integer $timeout = 10) : void

Parameters

$host

string

Host name or IP addres

$port

integer

TCP port number

$timeout

integer

Connection timeout in seconds

Destructor. Cleans up socket connection and command buffer

__destruct() : void

Attempts connection to remote host. Returns TRUE if sucessful.

connect() : boolean

Returns

boolean

Closes IP socket

disconnect() : boolean

Returns

boolean

Executes command and returns a string with result.

exec(string $command) : string

This method is a wrapper for lower level private methods

Parameters

$command

string

Command to execute

Returns

stringCommand result

Sets the string of characters to respond to.

setPrompt(string $s = '$') : boolean

This should be set to the last character of the command line prompt

Parameters

$s

string

String to respond to

Returns

boolean

Clears internal command buffer

clearBuffer() : void

Returns the content of the command buffer

getBuffer() : string

Returns

stringContent of the command buffer

Gets character from the socket

getc() : void

Telnet control character magic

negotiateTelnetOptions() : boolean

Returns

boolean

Reads characters from the socket and adds them to command buffer.

readTo(string $prompt) : boolean

Handles telnet control characters. Stops when prompt is ecountered.

Parameters

$prompt

string

Returns

boolean

Reads socket until prompt is encountered

waitPrompt() 

Write command to a socket

write(string $buffer, boolean $addNewLine = TRUE) : boolean

Parameters

$buffer

string

Stuff to write to socket

$addNewLine

boolean

Default true, adds newline to the command

Returns

boolean

 Properties

 

$DC1

$DC1 

Default

 

$DO

$DO 

Default

 

$DONT

$DONT 

Default

 

$IAC

$IAC 

Default

 

$NULL

$NULL 

Default

 

$WILL

$WILL 

Default

 

$WONT

$WONT 

Default

 

$buffer

$buffer 

Default

NULL
 

$errno

$errno 

Default

 

$errstr

$errstr 

Default

 

$host

$host 

Default

 

$port

$port 

Default

 

$prompt

$prompt 

Default

 

$socket

$socket 

Default

NULL
 

$timeout

$timeout 

Default

 Constants

 

TELNET_ERROR

TELNET_ERROR = FALSE 
 

TELNET_OK

TELNET_OK = TRUE