epiecs / phpmiko
PHP编写的Netmiko替代品。
Requires
- php: >=7.1
- phpseclib/phpseclib: ~3.0
Requires (Dev)
- phpunit/phpunit: ^8.3
Suggests
- ext-gmp: Install the GMP (GNU Multiple Precision) extension in order to speed up arbitrary precision integer arithmetic operations.
- ext-libsodium: SSH2/SFTP can make use of some algorithms provided by the libsodium-php extension.
- ext-mcrypt: Install the Mcrypt extension in order to speed up a few other cryptographic operations.
- ext-openssl: Install the OpenSSL extension in order to speed up a wide variety of cryptographic operations.
README
<3 NetMiko但我是PHP开发者。因此我决定构建一个PHP替代品
对Kirk Byers表示敬意,他创建了netmiko并为这个项目提供了巨大的灵感。
要立即开始,请查看快速入门示例。
要求
- Php >= 7.1
安装
composer require epiecs/phpmiko
支持的设备
已实现
- Aruba
- Cisco ios
- Cisco nx-os
- HPE Comware
- Juniper Junos
计划中
- Avaya
- Linux
- Vyos
- Checkpoint
- ...
支持的协议
- ssh
- telnet
内容
示例
快速入门示例
<?php require_once __DIR__ . '/vendor/autoload.php'; $device = new \Epiecs\PhpMiko\ConnectionHandler([ 'device_type' => 'junos', 'hostname' => '192.168.0.1', 'username' => 'my_user', 'password' => 'mysafepassword', ]); print_r($device->operation([ 'show system uptime', 'show system alarms', ])); /* Array ( [show system uptime] => fpc0: -------------------------------------------------------------------------- Current time: 2018-11-02 02:30:18 CET Time Source: LOCAL CLOCK System booted: 2018-09-15 09:22:02 CEST (6w5d 18:08 ago) Protocols started: 2018-09-15 09:24:40 CEST (6w5d 18:05 ago) Last configured: 2018-10-14 06:47:11 CEST (2w4d 20:43 ago) by itdept 2:30AM up 47 days, 18:08, 3 users, load averages: 0.11, 0.11, 0.08 [show system alarms] => No alarms currently active ) */ print_r($device->configure([ 'set interfaces ge-0/0/47 description "Test for documentation"', 'edit interfaces ge-0/0/46', 'set description "Sequential commands work"', ])); /* Array ( [set interfaces ge-0/0/47 description "Test for documentation"] => [edit interfaces ge-0/0/46] => [set description "Sequential commands work"] => ) */
连接到设备
<?php require_once __DIR__ . '/vendor/autoload.php'; $device = new \Epiecs\PhpMiko\ConnectionHandler([ 'device_type' => 'junos', 'hostname' => '192.168.0.1', 'username' => 'username', 'password' => 'password', 'protocol' => 'ssh', //default is ssh 'port' => 22, //defaults to the protocol default if not set 'secret' => 'secret', //default is '' 'verbose' => false, //default is false 'raw' => false //default is false ]);
当将raw标志设置为true时,PhpMiko不会清理输出并按原样返回(除了几个隐藏字符外,以便您至少可以获取所有文本输出)。
secret用于需要不同密码的运行级。例如,在Cisco ios的启用模式(特权执行)中。您会将启用密码放入秘密字段。
verbose当设置为true时,所有发送和接收的原始数据包都将输出以供调试目的。
--- output cut short for brevity, notice the arrows
-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0013, network: 0.0002s)
00000000 00:00:00:00:00:00:00:05:64:61:74:65:0a ........date.
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.1556, network: 0.0011s)
00000000 00:00:00:02:00:00:00:07:64:61:74:65:0d:0d:0a ........date...
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.1458, network: 0.0001s)
00000000 00:00:00:02:00:00:00:1f:46:72:69:20:4a:75:6e:20 ........Fri Jun
00000010 32:31:20:31:31:3a:31:36:3a:31:32:20:43:45:53:54 21 11:16:12 CEST
00000020 20:32:30:31:39:0d:0a 2019..
有关所有device_types的列表,请参阅设备类型和命令映射。
发送命令
在发送命令时,您可以提供字符串或数组。两种方式都可以。当提供数组时,命令将按顺序执行。
在这些示例中,使用了命令类型operation
。有关所有命令类型的详细信息,请参阅命令类型。
以字符串形式发送单个命令
print_r($device->operation('show interfaces ge-0/0/0'));
以数组形式发送单个命令
print_r($device->operation([ 'show interfaces ge-0/0/0', ]));
发送多个命令
print_r($device->operation([ 'show interfaces ge-0/0/0', 'show interfaces ge-0/0/1', ]));
输出
所有输出都将作为数组返回,其中键是运行的命令
Array
(
[show version] => fpc0:
--------------------------------------------------------------------------
Hostname: SW-Junos
Model: ex3300-48p
Junos: 15.1R7.9
JUNOS EX Software Suite [15.1R7.9]
JUNOS FIPS mode utilities [15.1R7.9]
JUNOS Online Documentation [15.1R7.9]
JUNOS EX 3300 Software Suite [15.1R7.9]
JUNOS Web Management Platform Package [15.1R7.9]
)
命令类型
PhpMiko有3种不同的机制来发送命令
cli
操作
配置
所有命令都是顺序执行并链式执行的。但是这仅限于提供的命令集。
尽管PhpMiko有3种机制,但并非所有机制都适用于每个设备。一些设备只有1个或2个配置层。有关概述,请参阅设备类型和命令映射。
cli
在设备上运行一个或多个CLI命令。例如,junos中的标准linux CLI或cisco ios的用户执行模式。
print_r($device->cli([ 'pwd', 'cd /var/www ; pwd ; ls -l' ]));
操作
在设备上运行一个或多个操作/启用命令。例如,junos中的CLI(操作)模式或cisco ios中的特权执行模式。
print_r($device->operation([ 'show interfaces terse', 'show configuration interfaces ge-0/0/0' ]));
配置
向设备发送一个或多个配置命令。例如,junos中的配置模式或cisco ios中的全局配置(配置终端)。
print_r($device->configure([ 'set interfaces ge-0/0/0 description "Test for documentation"', 'edit interfaces ge-0/0/1', 'set description "Sequential commands work"', ]));
设备类型和命令映射
1:仅在使用root帐户时才有效
清理和调试
设置原始模式
当调用函数时默认为true。
$device->raw(); $device->raw(false);
关闭连接
$device->disconnect();
建议
SSH连接的底层库是phpseclib。它们推荐以下PHP扩展,以便在使用SSH时获得良好的速度提升
- ext-libsodium
- ext-openssl
- ext-mcrypt
- ext-gmp
这些扩展不是必需的。