TimeTrex API Manual

Telnet
in package

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

Table of Contents

TELNET_ERROR  = \false
TELNET_OK  = \true
$buffer  : mixed
$DC1  : mixed
$DO  : mixed
$DONT  : mixed
$errno  : mixed
$errstr  : mixed
$host  : mixed
$IAC  : mixed
$NULL  : mixed
$port  : mixed
$prompt  : mixed
$socket  : mixed
$timeout  : mixed
$WILL  : mixed
$WONT  : mixed
__construct()  : mixed
Constructor. Initialises host, port and timeout parameters defaults to localhost port 23 (standard telnet port)
__destruct()  : void
Destructor. Cleans up socket connection and command buffer
connect()  : bool
Attempts connection to remote host. Returns TRUE if sucessful.
disconnect()  : bool
Closes IP socket
exec()  : string
Executes command and returns a string with result.
login()  : bool
Attempts login to remote host.
setPrompt()  : bool
Sets the string of characters to respond to.
clearBuffer()  : void
Clears internal command buffer
getBuffer()  : string
Returns the content of the command buffer
getc()  : bool|string|void
Gets character from the socket
negotiateTelnetOptions()  : bool
Telnet control character magic
readTo()  : bool
Reads characters from the socket and adds them to command buffer.
waitPrompt()  : mixed
Reads socket until prompt is encountered
write()  : bool
Write command to a socket

Constants

Properties

Methods

__construct()

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

public __construct([string $host = '127.0.0.1' ][, string $port = '23' ][, int $timeout = 10 ]) : mixed
Parameters
$host : string = '127.0.0.1'

Host name or IP addres

$port : string = '23'

TCP port number

$timeout : int = 10

Connection timeout in seconds

Tags
throws
Exception
Return values
mixed

__destruct()

Destructor. Cleans up socket connection and command buffer

public __destruct() : void
Return values
void

connect()

Attempts connection to remote host. Returns TRUE if sucessful.

public connect() : bool
Return values
bool

disconnect()

Closes IP socket

public disconnect() : bool
Return values
bool

exec()

Executes command and returns a string with result.

public exec(string $command) : string

This method is a wrapper for lower level private methods

Parameters
$command : string

Command to execute

Return values
string

Command result

login()

Attempts login to remote host.

public login(string $username, string $password[, string $login_prompt = 'login:' ][, string $password_prompt = 'Password:' ][, string $prompt = '#' ]) : bool

This method is a wrapper for lower level private methods and should be modified to reflect telnet implementation details like login/password and line prompts. Defaults to standard unix non-root prompts

Parameters
$username : string

Username

$password : string

Password

$login_prompt : string = 'login:'
$password_prompt : string = 'Password:'
$prompt : string = '#'
Tags
throws
Exception
Return values
bool

setPrompt()

Sets the string of characters to respond to.

public setPrompt([string $s = '$' ]) : bool

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

Parameters
$s : string = '$'

String to respond to

Return values
bool

clearBuffer()

Clears internal command buffer

private clearBuffer() : void
Return values
void

getBuffer()

Returns the content of the command buffer

private getBuffer() : string
Return values
string

Content of the command buffer

getc()

Gets character from the socket

private getc() : bool|string|void
Return values
bool|string|void

negotiateTelnetOptions()

Telnet control character magic

private negotiateTelnetOptions() : bool
Tags
throws
Exception
Return values
bool

readTo()

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

private readTo(string $prompt) : bool

Handles telnet control characters. Stops when prompt is ecountered.

Parameters
$prompt : string
Return values
bool

waitPrompt()

Reads socket until prompt is encountered

private waitPrompt() : mixed
Return values
mixed

write()

Write command to a socket

private write(string $buffer[, bool $addNewLine = true ]) : bool
Parameters
$buffer : string

Stuff to write to socket

$addNewLine : bool = true

Default true, adds newline to the command

Return values
bool

Search results