marbocub / network-equipment
网络设备的 Telnet 客户端和响应解析器
Requires
- php: >= 7.2.0
- graze/telnet-client: ^2.3
Requires (Dev)
- phpunit/phpunit: ^8
README
PHP 编写的用于网络设备的 Telnet 客户端和响应解析器。
此库提供以下两个功能。
- Telnet 客户端,扩展 graze/telnet-client 以提供 waitForPrompt(),login(),enable(),configure() 和针对网络设备的多个预设
- 解析器,将支持的 Cisco IOS 命令响应转换为数组
解析器作为与 Telnet 客户端集成的组件实现,也可以作为独立库使用。如果您想使用自己的 Telnet/SSH 客户端库,则可以将解析器作为独立库使用。
支持的网络设备
由解析器和 Telnet 客户端支持的网络设备包括
- Cisco IOS
- Cisco IOS-XE
Telnet 客户端可能在以下设备上工作
- Juniper JUNOS(需要自定义提示设置)
- Linux 和其他类似 UNIX 的平台(需要自定义提示设置)
- Alaxala 网络交换机
- Extreme XOS,HP Procurve,DLink 和其他智能网络交换机
解析器支持的 Cisco IOS 命令
- show interface status
- show interface counters
- show mac address-table
- show lldp neighbors
- show ip interface brief
- show arp
入门指南
安装
通过 Composer
composer require marbocub/network-equipment
Telnet 客户端的快速入门示例
以下示例登录到 Cisco IOS 交换机以获取并显示接口状态
<?php require_once("vendor/autoload.php"); use Marbocub\NetworkEquipment\TelnetClient; use Graze\TelnetClient\Exception\TelnetException; $telnet = TelnetClient::factory(); try { $telnet->connect("127.0.0.1:23"); $telnet->login("username", "password"); $telnet->execute("terminal length 0"); $response = $telnet->execute("show interface status"); echo $response; print_r($response->getResponseArray()); } catch (TelnetException $e) { echo $e->getMessage(); die(); }
独立解析器库的快速入门示例
以下示例解析从 Cisco IOS 交换机获取的 "show int status" 命令响应
<?php require_once("vendor/autoload.php"); use Marbocub\NetworkEquipment\ResponseParser; /* executed command and the response */ $command = "show int status"; $response = $yourSSHClient->execute($command); echo $response; /* start parse */ $parser = new ResponseParser(); $result = $parser->parse($command, $response); print_r($result);
示例结果
两个示例的结果是
Port Name Status Vlan Duplex Speed Type
Te1/0/1 description connected trunk full 10G SFP-10GBase-SR
Po1 connected trunk a-full a-1000
Array
(
[Te1/0/1] => Array
(
[Port] => 'Te1/0/1',
[Name] => 'description',
[Status] => 'connected',
[Vlan] => 'trunk',
[Duplex] => 'full',
[Speed] => '10G',
[Type] => 'SFP-10GBase-SR',
)
[Po1] => Array
(
[Port] => 'Po1',
[Name] => '',
[Status] => 'connected',
[Vlan] => 'trunk',
[Duplex] => 'a-full',
[Speed] => 'a-1000',
[Type] => '',
)
)
Telnet 客户端的用法
实例化客户端
要实例化客户端,请使用 TelnetClient::factory(),它返回一个 TelnetClient 实例
require_once("vendor/autoload.php"); use Marbocub\NetworkEquipment\TelnetClient; use Graze\TelnetClient\Exception\TelnetException; $telnet = TelnetClient::factory();
连接到网络设备并登录
要登录网络设备,请使用 connect() 方法首先创建一个套接字,然后使用 login() 方法
try { $telnet->connect("127.0.0.1:23"); $telnet->login("username", "password"); } catch (TelnetException $e) { /* failed */ }
注意:与graze/telnet-client相比,connect() 的区别在于预设的 Cisco IOS 提示
执行命令并解析响应
一旦连接,可以使用 execute() 方法将命令写入套接字并接收响应
try { $telnet->execute("terminal length 0"); $response = $telnet->execute("show interface status"); // Getting the response text echo $response; // Getting the parsed array print_r($response->getResponseArray()); } catch (TelnetException $e) { /* failed */ }
注意:对于 Cisco IOS,必须首先执行 "terminal length 0"。
注意:getResponseArray() 内部调用解析器。仅适用于支持 Cisco IOS 命令的执行。
为 Cisco IOS 打开特权模式
要为 Cisco IOS 打开特权模式,请使用 enable() 方法
try { $telnet->enable("password"); } catch (TelnetException $e) { /* failed */ }
批量执行 Cisco IOS 配置命令(必须打开特权模式)
要配置 Cisco IOS,请使用 configure() 方法
$commands = [ 'interface Gi1/0/1', 'switchport access vlan 100', ]; try { $telnet->configure($commands); } catch (TelnetException $e) { /* failed */ }
注意:configure() 的第一个参数是一个数组,列出了要执行的命令。
自定义提示设置
您可以在 connect() 的第二个参数中指定以下提示常量。
- 对于 Juniper JUNOS
$telnet->connect("127.0.0.1:23', TelnetClient::PROMPT_JUNOS);
- 对于 Linux 或其他 UNIX 平台
$telnet->connect("127.0.0.1:23', TelnetClient::PROMPT_SHELL);
解析器的用法
实例化解析器
require_once("vendor/autoload.php"); use Marbocub\NetworkEquipment\ResponseParser; $parser = new ResponseParser();
解析命令响应
parse() 的第一个参数是执行的 Cisco IOS 命令。对于解析器选择格式是必需的。
$result = $parser->parse( "executed command", "response text" );
限制
本库的Telnet客户端需要PHP的底层套接字扩展(ext-sockets)。
作者
- marbocub - 初始工作
许可证
本项目采用MIT许可证 - 有关详细信息,请参阅LICENSE文件。