|
1 | 1 | # telnet-client
|
2 |
| -☎️ A telnet client written in PHP |
| 2 | + |
| 3 | +[](https://packagist.org/packages/graze/telnet-client) |
| 4 | +[](LICENSE.md) |
| 5 | +[](https://travis-ci.org/graze/telnet-client) |
| 6 | +[](https://scrutinizer-ci.com/g/graze/telnet-client/code-structure) |
| 7 | +[](https://scrutinizer-ci.com/g/graze/telnet-client) |
| 8 | +[](https://packagist.org/packages/graze/telnet-client) |
| 9 | + |
| 10 | +A telnet clinet written in PHP |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +Via Composer |
| 15 | + |
| 16 | +``` bash |
| 17 | +$ composer require graze/telnet-client |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +### Instantiating a client |
| 23 | + |
| 24 | +Use the `build` method to return a `TelnetClientInterface` instance. This will hold an open socket to the remote sever identified by `$dsn`. |
| 25 | +``` php |
| 26 | +$dsn = '127.0.0.1:23'; |
| 27 | +$client = Graze\TelnetClient\TelnetClient::build($dsn); |
| 28 | +... |
| 29 | +``` |
| 30 | + |
| 31 | +### Issuing commands |
| 32 | + |
| 33 | +With the client instantiated, the `execute` method can be used to write `$command` to the socket: |
| 34 | + |
| 35 | +``` php |
| 36 | +... |
| 37 | +$command = 'Open the pod bay doors, HAL'; |
| 38 | +$resp = $client->execute($command); |
| 39 | +... |
| 40 | +``` |
| 41 | + |
| 42 | +### Responses |
| 43 | +Once a command has been sent, the socket is read until a specific sequence is encountered. This is a line ending immediately preceeded by either a prompt or an error prompt. |
| 44 | +At this point the `execute` method returns a `TelnetResponseInterface` object: |
| 45 | + |
| 46 | +```php |
| 47 | +/** |
| 48 | + * Whether an error prompt was encountered. |
| 49 | + * |
| 50 | + * @return bool |
| 51 | + */ |
| 52 | +public function isError(); |
| 53 | + |
| 54 | +/** |
| 55 | + * Any response from the server up until a prompt is encountered. |
| 56 | + * |
| 57 | + * @return string |
| 58 | + */ |
| 59 | +public function getResponseText(); |
| 60 | + |
| 61 | +/** |
| 62 | + * The portion of the server's response that caused execute() to return. |
| 63 | + * |
| 64 | + * @return array |
| 65 | + */ |
| 66 | +public function getPromptMatches(); |
| 67 | +``` |
| 68 | + |
| 69 | +A success response object might look like: |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +Or if the server responded with an error: |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +**Note:** `responseText` and `promptMatches` are trimmed of line endings. |
| 78 | +### Client configuration |
| 79 | +The client uses the following defaults: |
| 80 | +* standard prompt `$` |
| 81 | +* error prompt `ERROR` |
| 82 | +* line endings `\n` |
| 83 | + |
| 84 | +Custom configuration can be passed to the `build` method like so: |
| 85 | +``` php |
| 86 | +$dsn = '127.0.0.1:23'; |
| 87 | +$prompt = 'OK'; |
| 88 | +$promptError = 'ERR'; |
| 89 | +$lineEnding = "\r\n"; |
| 90 | +$client = Graze\TelnetClient\TelnetClient::build($dsn, $prompt, $promptError, $lineEnding); |
| 91 | +... |
| 92 | +``` |
| 93 | + |
| 94 | +The client's global `$prompt` can be temporarily overridden on a per-execute basis: |
| 95 | +``` php |
| 96 | +... |
| 97 | +$command = 'login'; |
| 98 | +$prompt = 'Username:'; |
| 99 | +$resp = $client->execute($command, $prompt); |
| 100 | +... |
| 101 | +``` |
| 102 | + |
| 103 | +### Complex prompts |
| 104 | +Some operations may respond with a more complex prompt. These instances can be handled by using a [regular expression](http://www.regular-expressions.info) to match the prompt. |
| 105 | +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: |
| 106 | + |
| 107 | +``` php |
| 108 | +$dsn = '127.0.0.1:23'; |
| 109 | +$promptError = 'ERROR [0-9]'; |
| 110 | +$client = Graze\TelnetClient\TelnetClient::build($dsn, null, $promptError); |
| 111 | +... |
| 112 | +``` |
| 113 | + |
| 114 | +An error response would look like: |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +We can take the regex one further by using a [named capturing group](http://www.regular-expressions.info/named.html), this makes the error code easily available to us in the `$promptMatches` array. |
| 119 | + |
| 120 | +```php |
| 121 | +$dsn = '127.0.0.1:23'; |
| 122 | +$promptError = 'ERROR (?<errorNum>[0-9])'; |
| 123 | +$client = Graze\TelnetClient\TelnetClient::build($dsn, null, $promptError); |
| 124 | +... |
| 125 | +``` |
| 126 | + |
| 127 | +which gives us: |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +**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). |
| 132 | +### Socket settings |
| 133 | +For timouts and more, PHP's `socket_set_option` is exposed via |
| 134 | +```php |
| 135 | +... |
| 136 | +$client->getSocket()->setOption() |
| 137 | +``` |
| 138 | + |
| 139 | +See [clue/php-socket-raw](https://github.com/clue/php-socket-raw) and [socket_set_option](http://php.net/manual/en/function.socket-set-option.php) for more info. |
| 140 | + |
| 141 | +## Change log |
| 142 | + |
| 143 | +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. |
| 144 | + |
| 145 | +## Testing |
| 146 | + |
| 147 | +``` bash |
| 148 | +$ make test |
| 149 | +``` |
| 150 | + |
| 151 | +## Contributing |
| 152 | + |
| 153 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 154 | + |
| 155 | +## Security |
| 156 | + |
| 157 | +If you discover any security related issues, please email john@graze.com instead of using the issue tracker. |
| 158 | + |
| 159 | +## Inspired by |
| 160 | + |
| 161 | +Based on [bestnetwork/Telnet](https://github.com/bestnetwork/Telnet). |
| 162 | + |
| 163 | +## Credits |
| 164 | + |
| 165 | +- [John Smith](https://github.com/john-n-smith) |
| 166 | +- Bestnetwork <reparto.sviluppo@bestnetwork.it> |
| 167 | +- Dalibor Andzakovic <dali@swerve.co.nz> |
| 168 | +- Marc Ennaji |
| 169 | +- Matthias Blaser <mb@adfinis.ch> |
| 170 | +- Christian Hammers <chammers@netcologne.de> |
| 171 | +- [All Contributors](../../contributors) |
| 172 | + |
| 173 | +## License |
| 174 | + |
| 175 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments