Telnet - staffcheck/api GitHub Wiki

Telnet

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 [email protected] Based on the code originally written by Marc Ennaji and extended by Matthias Blaser [email protected]

  • Class name: Telnet
  • Namespace:

Constants

TELNET_ERROR

const TELNET_ERROR = FALSE

TELNET_OK

const TELNET_OK = TRUE

Properties

$host

private mixed $host
  • Visibility: private

$port

private mixed $port
  • Visibility: private

$timeout

private mixed $timeout
  • Visibility: private

$socket

private mixed $socket = NULL
  • Visibility: private

$buffer

private mixed $buffer = NULL
  • Visibility: private

$prompt

private mixed $prompt
  • Visibility: private

$errno

private mixed $errno
  • Visibility: private

$errstr

private mixed $errstr
  • Visibility: private

$NULL

private mixed $NULL
  • Visibility: private

$DC1

private mixed $DC1
  • Visibility: private

$WILL

private mixed $WILL
  • Visibility: private

$WONT

private mixed $WONT
  • Visibility: private

$DO

private mixed $DO
  • Visibility: private

$DONT

private mixed $DONT
  • Visibility: private

$IAC

private mixed $IAC
  • Visibility: private

Methods

__construct

void Telnet::__construct(string $host, integer $port, integer $timeout)

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

  • Visibility: public

Arguments

  • $host string - Host name or IP addres
  • $port integer - TCP port number
  • $timeout integer - Connection timeout in seconds

__destruct

void Telnet::__destruct()

Destructor. Cleans up socket connection and command buffer

  • Visibility: public

connect

boolean Telnet::connect()

Attempts connection to remote host. Returns TRUE if sucessful.

  • Visibility: public

disconnect

boolean Telnet::disconnect()

Closes IP socket

  • Visibility: public

exec

string Telnet::exec(string $command)

Executes command and returns a string with result.

This method is a wrapper for lower level private methods

  • Visibility: public

Arguments

  • $command string - Command to execute

login

boolean Telnet::login(string $username, string $password, $login_prompt, $password_prompt, $prompt)

Attempts login to remote host.

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

  • Visibility: public

Arguments

  • $username string - Username
  • $password string - Password
  • $login_prompt mixed
  • $password_prompt mixed
  • $prompt mixed

setPrompt

boolean Telnet::setPrompt(string $s)

Sets the string of characters to respond to.

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

  • Visibility: public

Arguments

  • $s string - String to respond to

getc

boolean|string|void Telnet::getc()

Gets character from the socket

  • Visibility: private

clearBuffer

void Telnet::clearBuffer()

Clears internal command buffer

  • Visibility: private

readTo

boolean Telnet::readTo(string $prompt)

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

Handles telnet control characters. Stops when prompt is ecountered.

  • Visibility: private

Arguments

  • $prompt string

write

boolean Telnet::write(string $buffer, boolean $addNewLine)

Write command to a socket

  • Visibility: private

Arguments

  • $buffer string - Stuff to write to socket
  • $addNewLine boolean - Default true, adds newline to the command

getBuffer

string Telnet::getBuffer()

Returns the content of the command buffer

  • Visibility: private

negotiateTelnetOptions

boolean Telnet::negotiateTelnetOptions()

Telnet control character magic

  • Visibility: private

waitPrompt

mixed Telnet::waitPrompt()

Reads socket until prompt is encountered

  • Visibility: private
⚠️ **GitHub.com Fallback** ⚠️