2017 © Pedro Peláez
 

library telnet-client

Telnet client written in PHP

image

graze/telnet-client

Telnet client written in PHP

  • Wednesday, March 14, 2018
  • by graze
  • Repository
  • 11 Watchers
  • 14 Stars
  • 15,548 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 4 Forks
  • 3 Open issues
  • 6 Versions
  • 11 % Grown

The README.md

telnet-client

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads, (*1)

A telnet client written in PHP, (*2)

Install

Via Composer, (*3)

``` bash composer require graze/telnet-client, (*4)


## Usage ### Instantiating a client Use the `factory` method to return a `TelnetClientInterface` instance: ``` php $client = Graze\TelnetClient\TelnetClient::factory();

Issuing commands

Connect to the remote endpoint using the connect method:, (*5)

``` php $dsn = '127.0.0.1:23'; $client->connect($dsn);, (*6)


Once connected, the `execute` method can be used to write `$command` to the socket: ``` php $command = 'Open the pod bay doors, HAL'; $resp = $client->execute($command);

Responses

Once a command has been sent, the socket is read until a specific sequence is encountered. This is a line ending immediately preceded by either a prompt, or an error prompt. At this point the execute method returns a TelnetResponseInterface object:, (*7)

/**
 * Whether an error prompt was encountered.
 *
 * @return bool
 */
public function isError();

/**
 * Any response from the server up until a prompt is encountered.
 *
 * @return string
 */
public function getResponseText();

/**
 * The portion of the server's response that caused execute() to return.
 *
 * @return array
 */
public function getPromptMatches();

A success response object might look like:, (*8)

``` php Graze\TelnetClient\TelnetResponse {#2 #isError: false #responseText: "Affirmative, Dave" #promptMatches: array:1 [ 0 => "$" ] }, (*9)


Or if the server responded with an error: ``` php Graze\TelnetClient\TelnetResponse {#2 #isError: true #responseText: " I'm sorry, Dave. I'm afraid I can't do that" #promptMatches: array:1 [ 0 => "ERROR" ] }

Note: responseText and promptMatches are trimmed of line endings., (*10)

Client configuration

The client uses the following defaults:, (*11)

  • standard prompt $
  • error prompt ERROR
  • line endings \n

Custom configuration can be passed to the connect method like so:, (*12)

``` php $dsn = '127.0.0.1:23'; $prompt = 'OK'; $promptError = 'ERR'; $lineEnding = "\r\n"; $client->connect($dsn, $prompt, $promptError, $lineEnding);, (*13)


The client's global `$prompt` can be temporarily overridden on a per-execute basis: ``` php $command = 'login'; $prompt = 'Username:'; $resp = $client->execute($command, $prompt);

Complex prompts

Some operations may respond with a more complex prompt. These instances can be handled by using a regular expression to match the prompt. For instance, a server may respond with ERROR n (where n is an integer) when an error condition is encountered. The client could be configured as such:, (*14)

``` php $dsn = '127.0.0.1:23'; $promptError = 'ERROR [0-9]'; $client->connect($dsn, null, $promptError);, (*15)


An error response would look like: ``` php Graze\TelnetClient\TelnetResponse {#2 #isError: true #responseText: "unknown command" #promptMatches: array:1 [ 0 => "ERROR 6" ] }

We can take the regex one further by using a named capturing group, this makes the error code easily available to us in the $promptMatches array., (*16)

$dsn = '127.0.0.1:23';
$promptError = 'ERROR (?<errorNum>[0-9])';
$client->connect($dsn, null, $promptError);

which gives us:, (*17)

``` php Graze\TelnetClient\TelnetResponse {#2 #isError: true #responseText: "unknown command" #promptMatches: array:3 [ 0 => "ERROR 6", "errorNum" => "6", 1 => "6" ] }, (*18)


**Note:** it's important to escape any characters in your regex that may have special meaning when interpreted by [preg_match](http://php.net/manual/en/function.preg-match.php). ### Socket settings For timeouts and more, PHP's `socket_set_option` is exposed via ```php $client->getSocket()->setOption();

See clue/php-socket-raw and socket_set_option for more info., (*19)

Change log

Please see CHANGELOG for more information what has changed recently., (*20)

Testing

bash make test, (*21)

Contributing

Please see CONTRIBUTING for details., (*22)

Security

If you discover any security related issues, please email security@graze.com instead of using the issue tracker., (*23)

Inspired by

Based on bestnetwork/Telnet., (*24)

Credits

License

The MIT License (MIT). Please see License File for more information., (*25)

The Versions

14/03 2018

dev-master

9999999-dev https://github.com/graze/telnet-client

Telnet client written in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar bestnetwork
by Dalibor Andzakovic
by Marc Ennaji
by Matthias Blaser
by Christian Hammers

telnet php client graze telnet-client

14/03 2018

v2.0.2

2.0.2.0 https://github.com/graze/telnet-client

Telnet client written in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar bestnetwork
by Dalibor Andzakovic
by Marc Ennaji
by Matthias Blaser
by Christian Hammers

telnet php client graze telnet-client

05/03 2018

v2.0.1

2.0.1.0 https://github.com/graze/telnet-client

Telnet client written in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar bestnetwork
by Dalibor Andzakovic
by Marc Ennaji
by Matthias Blaser
by Christian Hammers

telnet php client graze telnet-client

13/02 2018

dev-dockeriose

dev-dockeriose https://github.com/graze/telnet-client

Telnet client written in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar bestnetwork
by Dalibor Andzakovic
by Marc Ennaji
by Matthias Blaser
by Christian Hammers

telnet php client graze telnet-client

01/03 2016

v2.0.0

2.0.0.0 https://github.com/graze/telnet-client

Telnet client written in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar bestnetwork
by Dalibor Andzakovic
by Marc Ennaji
by Matthias Blaser
by Christian Hammers

telnet php client graze telnet-client

18/02 2016

v1.0.0

1.0.0.0 https://github.com/graze/telnet-client

Telnet client written in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar bestnetwork
by Dalibor Andzakovic
by Marc Ennaji
by Matthias Blaser
by Christian Hammers

telnet php client graze telnet-client