graze/telnet-client
Composer 安装命令:
composer require graze/telnet-client
包简介
Telnet client written in PHP
README 文档
README
A telnet client written in PHP
Install
Via Composer
composer require graze/telnet-client
Usage
Instantiating a client
Use the factory method to return a TelnetClientInterface instance:
$client = Graze\TelnetClient\TelnetClient::factory();
Issuing commands
Connect to the remote endpoint using the connect method:
$dsn = '127.0.0.1:23'; $client->connect($dsn);
Once connected, the execute method can be used to write $command to the socket:
$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:
/** * 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:
Graze\TelnetClient\TelnetResponse {#2 #isError: false #responseText: "Affirmative, Dave" #promptMatches: array:1 [ 0 => "$" ] }
Or if the server responded with an error:
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.
Client configuration
The client uses the following defaults:
- standard prompt
$ - error prompt
ERROR - line endings
\n
Custom configuration can be passed to the connect method like so:
$dsn = '127.0.0.1:23'; $prompt = 'OK'; $promptError = 'ERR'; $lineEnding = "\r\n"; $client->connect($dsn, $prompt, $promptError, $lineEnding);
The client's global $prompt can be temporarily overridden on a per-execute basis:
$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:
$dsn = '127.0.0.1:23'; $promptError = 'ERROR [0-9]'; $client->connect($dsn, null, $promptError);
An error response would look like:
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.
$dsn = '127.0.0.1:23'; $promptError = 'ERROR (?<errorNum>[0-9])'; $client->connect($dsn, null, $promptError);
which gives us:
Graze\TelnetClient\TelnetResponse {#2 #isError: true #responseText: "unknown command" #promptMatches: array:3 [ 0 => "ERROR 6", "errorNum" => "6", 1 => "6" ] }
Note: it's important to escape any characters in your regex that may have special meaning when interpreted by preg_match.
Socket settings
For timeouts and more, PHP's socket_set_option is exposed via
$client->getSocket()->setOption();
See clue/php-socket-raw and socket_set_option for more info.
Change log
Please see CHANGELOG for more information what has changed recently.
Testing
make test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email security@graze.com instead of using the issue tracker.
Inspired by
Based on bestnetwork/Telnet.
Credits
- John Smith
- Bestnetwork reparto.sviluppo@bestnetwork.it
- Dalibor Andzakovic dali@swerve.co.nz
- Marc Ennaji
- Matthias Blaser mb@adfinis.ch
- Christian Hammers chammers@netcologne.de
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
graze/telnet-client 适用场景与选型建议
graze/telnet-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 245.03k 次下载、GitHub Stars 达 48, 最近一次更新时间为 2016 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「client」 「telnet」 「graze」 「telnet-client」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 graze/telnet-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 graze/telnet-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 graze/telnet-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Telnet client in php
Mock PSR-18 HTTP client
Cisco IOS communications and configuration parsing library
Asynchronous MQTT client built on React
Telnet client in php
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
统计信息
- 总下载量: 245.03k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 50
- 点击次数: 17
- 依赖项目数: 8
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-18